加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在watch os 2中运行时更新wkinterfacecontroller

发布时间:2020-12-16 10:11:36 所属栏目:百科 来源:网络整理
导读:在头文件中 #import WatchKit/WatchKit.h#import Foundation/Foundation.h#import WatchConnectivity/WatchConnectivity.h@interface InterfaceController : WKInterfaceControllerWCSessionDelegate- (IBAction)lastSongButtonClick;- (IBAction)playSongBu
在头文件中

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import <WatchConnectivity/WatchConnectivity.h>

@interface InterfaceController : WKInterfaceController<WCSessionDelegate>
- (IBAction)lastSongButtonClick;
- (IBAction)playSongButtonClick;
- (IBAction)nextSongButtonClick;
@property (strong,nonatomic) IBOutlet WKInterfaceLabel *songTitleLabel;
@property (strong,nonatomic) IBOutlet WKInterfaceButton *playSongButton;

@end

所以我实现了WCSessionDelegate,每次收到关于UI的内容时,我都希望它能够更新.所以在我的.m文件中我有:

- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message{
    NSString* type = [message objectForKey:@"type"];
    if([type isEqualToString:@"UIUpdateInfo"]){
        NSLog(@"?Watch receives UI update info");
        [self handleUIUpdateInfo:[message objectForKey:@"content"]];
    }
}

- (void)handleUIUpdateInfo:(NSDictionary*)updateInfo{
    [self.songTitleLabel setText:[updateInfo objectForKey:@"nowPlayingSongTitle"]];
    [self.playSongButton setBackgroundImage:[updateInfo objectForKey:@"playButtonImage"]];
}

但是,它似乎没有更新.有没有适当的更新方式?

解决方法

你在那里一半.您已配置正确地在手表端接收消息,但是您需要在更新UI时触发要发送的消息(因此触发didReceiveMessage以执行和更新相应的内容).

无论您在哪里更改UI,都需要包含以下内容:

NSDictionary *message = //dictionary of info you want to send
[[WCSession defaultSession] sendMessage:message
                           replyHandler:^(NSDictionary *reply) {
                               //handle reply didReceiveMessage here
                           }
                           errorHandler:^(NSError *error) {
                               //catch any errors here
                           }
 ];

另外,请确保正确激活WCSession.这通常在viewDidLoad或willAppear中完成,具体取决于您是在手机还是手表上实现此功能.

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }
}

您可以在本教程中看到完整的端到端Watch to iPhone数据传输示例 – http://www.kristinathai.com/watchos-2-tutorial-using-sendmessage-for-instantaneous-data-transfer-watch-connectivity-1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读