xcode - 如何在WatchKit中重用WKInterfaceTable

标签 xcode watchkit apple-watch

我为 WatchKit 应用选择了基于页面的布局。每页显示一个表格。我想在所有页面上保持表格的布局完全相同。我将在运行时将过滤后的数据提供给每个页面,因此基本上页面将是相同的,只是数据不同。

enter image description here

实现此目的的一种方法是在 InterfaceBuilder 中为每个页面手动创建 InterfaceController 实例,然后填充 GUI 元素并连接 socket 。这里的问题是,每当我想要更改某些内容(例如,移动标签或添加按钮)时,我都必须将该更改一致地应用到每个页面。

此外,对于每个页面,我都必须将 socket 连接到表行 Controller ,本质上是一遍又一遍地重复自己。这是一个例子:

enter image description here

有没有办法重用表?

我考虑过继承,但是documentation告诉我不要子类化 WKInterfaceTable。这也排除了以编程方式创建表的可能性。

最佳答案

您应该使用表和各种行类型创建一个界面 Controller 。然后你想创建一个与我的答案非常相似的架构here 。您只有一个 PageOneInterfaceController,在此示例中应将其命名为 TableInterfaceController,并使用 TableInterfaceIdentifier 作为标识符。然后您将执行以下操作:

MainInterfaceController.swift

func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    let context1 = "context 1 - for you to fill out"
    let context2 = "context 2 - for you to fill out"
    let context3 = "context 3 - for you to fill out"

    WKInterfaceController.reloadRootControllersWithNames(
        ["TableInterfaceIdentifier", "TableInterfaceIdentifier", "TableInterfaceIdentifier"], 
        contexts: [context1, context2, context3]
    )
}

这将使用同一界面 Controller 重新加载页面集,该界面 Controller 显示相应上下文中的所有数据。

关于xcode - 如何在WatchKit中重用WKInterfaceTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973471/

相关文章:

ios - 测试飞行 - 本地网络权限,TestFlight 构建中出现 NSBonjourServices 错误

ios - 如何判断通知是否可以发送到Apple Watch?

ios - 我需要 Apple Watch 应用程序才能在 iPhone 和 Apple Watch 之间进行通信吗?

ios - 安装 resigned watch os 应用程序时出错

ios - Firebase:FIRApp.configure() 一直要我将其更改为 FirebaseApp.configure()

ios - xcodebuild代码签名错误: No matching codesigning identity found:

app-store-connect - 存档上的 WatchKit2 错误 - WatchKit 应用程序不包含任何 WatchKit 扩展

swift - 函数顺序 - 哪个代码何时运行?

ios - 在 Apple Watch 中使用 animateImageWithImages 时何时停止动画

ios - Storyboard的 Swift 输入和事件处理