0

我创建了一个 iPhone 应用程序(带视图)/手表(带界面),它显示速度、距离和带有播放/暂停、停止和清除的计时器。

我曾经在(发送和接收上下文)之间共享 WatchConnectivity 应用程序上下文数据。到目前为止一切正常,但我会添加另一个页面/界面,在这些页面/界面上与 iPhone 交换 Watch the Context,我根本不知道如何。

我的界面故事板

如果我打开 Session 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 回答