【问题标题】:WatchKit thread issue between classes类之间的 WatchKit 线程问题
【发布时间】:2015-12-14 05:08:37
【问题描述】:

我有两个名为InterfaceControllerLoadInterfaceController 的类。

我正在通过我的LoadInterfaceController 拨打InterfaceControlleruiChange function

InterfaceController *interfaceController = [InterfaceController alloc];
//[interfaceController performSelectorOnMainThread:@selector(uiChange) withObject:nil waitUntilDone:true];
[interfaceController uiChange];

function 被调用,但InterfaceController 中的 UI 未被修改。

- (void)uiChange {
    NSLog(@"uiChange was called");
    //... make changes to the UI ...
}

如果从源自InterfaceController classfunction 调用function,则分别更改UI。

我尝试在main thread 上调用uiChange(如here 所述),但用户界面没有响应。如何指定用于InterfaceController 的 UI 的线程?

【问题讨论】:

  • 如果你调用的是 UI 相关的 API,那么它们必须在主线程上调用。
  • 我已经尝试在主线程上调用它。 stackoverflow.com/questions/11582223/…
  • "try" 听起来不像你还在做。您必须使用主线程并从那里开始。
  • performSelectorOnMainThreadNSOperationQueuedispatch_async 不能解决问题。 NSThread 报告它正在 main thread 上运行。

标签: objective-c class watchkit nsthread watchos-2


【解决方案1】:

here 相同的问题。不要自己初始化控制器,让 Watch 在用户流程发生时进行。

我建议在您的架构中添加 Pub/Sub 模式。 NSNotificationCenter 类是一个很好的例子。它允许应用程序的各个部分相互通信,并且它也经常用于控制器通信。

Here is 是我最近回答另一个问题时提供的 AppDelegate 和控制器之间通信的一个很好的例子。但如果你真的需要,我可以采用它作为你的示例。

【讨论】:

  • 即使使用NSNotificationCenter在类之间进行通信,问题仍然存在。
  • @Aleksander Azizi - 你应该详细说明你的任务。您为什么要尝试触发当前未显示的另一个控制器的 UI 更改?将InterfaceController 类的willActivate() 方法与上一个问题中的单例模型结合使用,在加载前初始化您的UI。
  • 我应该承认在 iOS 上使用 NSNotificationCenter 更容易接受嵌套的 `UIViewControllers' 使用,换句话说,多个控制器同时显示如下:stackoverflow.com/a/17499426/3242031 尝试使用上一条评论中的方法。
  • LoadInterfaceController 传递数据后,需要更新InterfaceControllerWKInterfaceTable。这是通过前面提到的uiChange 函数进行管理的。
  • 抱歉,我对你的任务感到困惑。我觉得你做错了什么,但无论如何我很高兴这对你有用。
猜你喜欢
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多