0

再生/一時停止、停止、クリアで速度、距離、タイマーを表示する iPhone アプリケーション (ビュー付き) / Watch (インターフェース付き) を作成します。

以前は、WatchConnectivity アプリケーション コンテキスト データを (コンテキストの送信と受信) 間で共有していました。これまでのところすべてが機能していますが、別のページ/インターフェイスを追加して、iPhone でコンテキストを監視し、方法がまったくわかりません。

マイ インターフェイス ストーリーボード

セッション 2 インターフェイスをオンにすると、情報が失われます

これが私の現在のコードです。2番目のインターフェイスでラベル TimeLabel、distanceLabel ラベル、および速度の表示を切り替えたい

//
//  InterfaceController.m
//  Watch Extension
//
//  Created by Arnaud Roy on 25/10/2015.
//  Copyright © 2015 Burotica. All rights reserved.
//

#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>

@interface InterfaceController() <WCSessionDelegate>
    @property (strong, nonatomic) WCSession *session;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *startLabel;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *timeLabel;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *distanceLabel;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *vitesseLabel;
    @property (nonatomic,assign) BOOL running;
@end


@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    // Configure interface objects here.
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
        //NSLog(@"SESSION AVAIBLE");
    }
    //Objective-C
    if ([[WCSession defaultSession] isReachable]) {
        //NSLog(@"SESSION REACHABLE");
    }
    self.running = false;
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (IBAction)startButton {
    if(self.running == false) {
        [self sendCmd:@"start"];
        [self.startLabel setTitle:[NSString stringWithFormat:@"PAUSE"]];
        self.running = true;
    }
    else
    {
        [self sendCmd:@"pause"];
        [self.startLabel setTitle:[NSString stringWithFormat:@"PLAY"]];
        self.running = false;
    }
}

- (IBAction)clearButton {
   [self sendCmd:@"clear"];
}

- (IBAction)plusmoinsButton {
   [self sendCmd:@"plusmoins"];
}

- (IBAction)stopButton {
    [self sendCmd:@"stop"];
}

-(void)sendCmd:(NSString*) cmdSendW{
    WCSession *session = [WCSession defaultSession];
    NSError *error;
    [session updateApplicationContext:@{@"cmdSendW":cmdSendW} error:&error];
}

- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
    NSString *cmdSend = [applicationContext objectForKey:@"cmdSend"];
    NSString *timeSend = [applicationContext objectForKey:@"timeSend"];
    NSString *distanceSend = [applicationContext objectForKey:@"distanceSend"];
    NSString *partielSend = [applicationContext objectForKey:@"partielSend"];
    NSString *vitesseSend = [applicationContext objectForKey:@"vitesseSend"];

    dispatch_async(dispatch_get_main_queue(), ^{
        if([cmdSend isEqual: @"start"])
        {
            [self.startLabel setTitle:[NSString stringWithFormat:@"PAUSE"]];
            [self.timeLabel setText:[NSString stringWithFormat:@"Time : %@", timeSend]];
            [self.distanceLabel setText:[NSString stringWithFormat:@"Distance : %@", distanceSend]];
            [self.vitesseLabel setText:[NSString stringWithFormat:@"Vitesse : %@", vitesseSend]];
            self.running = true;
        }
        else if([cmdSend isEqual: @"pause"])
        {
            [self.startLabel setTitle:[NSString stringWithFormat:@"PLAY"]];
            [self.timeLabel setText:[NSString stringWithFormat:@"Time : %@", timeSend]];
            [self.distanceLabel setText:[NSString stringWithFormat:@"Distance : %@", distanceSend]];
            [self.vitesseLabel setText:[NSString stringWithFormat:@"Vitesse : %@", vitesseSend]];
            self.running = false;
        }
        else if([cmdSend isEqual: @"stop"])
        {
            [self.startLabel setTitle:[NSString stringWithFormat:@"PLAY"]];
            [self.timeLabel setText:[NSString stringWithFormat:@"Time : %@", timeSend]];
            [self.distanceLabel setText:[NSString stringWithFormat:@"Distance : %@", distanceSend]];
            [self.vitesseLabel setText:[NSString stringWithFormat:@"Vitesse : %@", vitesseSend]];
            self.running = false;
        }
    });

}

@end

ご協力いただきありがとうございます

4

1 に答える 1

0

UIApplicationDelegate と ExtensionDelegate で、WCSessionDelegate である新しいオブジェクトのインスタンスを作成することをお勧めします。このオブジェクトは、受信データをディスクに保持し、バッキング データ ストアが更新されたという通知を送信します。その後、各 UI コンポーネントはこれらの通知を監視し、必要に応じて UI を更新できます。

于 2015-10-27T18:02:42.063 に答える