从WatchKit中的模态视图传回数据 [英] Passing data back from a modal view in WatchKit
问题描述
当模态呈现或推送接口控制器时,我们可以指定 context
参数,将一些数据传递给新控制器,如下所示。
When modally presenting, or pushing, an interface controller we can specify the context
parameter to pass some data to the new controller as follows.
// Push
[self pushControllerWithName:@"MyController" context:[NSDictionary dictionaryWithObjectsAndKeys:someObject, @"someKey", ..., nil]];
// Modal
[self presentControllerWithName:@"MyController" context:[NSDictionary dictionaryWithObjectsAndKeys:someObject, @"someKey", ..., nil]];
我的问题是,我们如何反过来?
My question is, how can we do the reverse?
假设我们以模态方式呈现一个控制器,以便用户从列表中选择一个项目,然后我们返回主控制器,我们如何获取已被选中的项目?
Say we present a controller modally for the user to pick an item from a list and we return to the main controller, how can we get the item that has been picked?
推荐答案
我写了一个在WatchKit中使用Delegation的完整示例,在上下文中传递委托实例,并从中调用委托函数模式:以下是GitHub上的完整项目示例
I wrote a full example that uses Delegation in WatchKit, passing the delegate instance in the context, and calling delegate function from the modal : Here is the full project example on GitHub
以下是示例的原理类:
InterfaceController.swift
这是主控制器,他的视图上有一个标签和一个按钮。按下按钮时,会调用 presentItemChooser
并显示ModalView(ModalInterfaceController)。我将上下文中的 InterfaceController
的实例传递给模态。重要的是这个控制器实现`ModalItemChooserDelegate'函数(协议定义在模态文件中)
This is the main Controller, there are a label and a button on his view. When you press the button, the presentItemChooser
get called and it present the ModalView (ModalInterfaceController). I pass the instance of InterfaceController
in the context to the modal. Important this controller implements `ModalItemChooserDelegate' functions (the protocol definition is in the modal file)
class InterfaceController: WKInterfaceController, ModalItemChooserDelegate {
@IBOutlet weak var itemSelected: WKInterfaceLabel!
var item = "No Item"
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
itemSelected.setText(item)
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func didSelectItem(itemSelected: String) {
self.item = itemSelected
}
@IBAction func presentItemChooser() {
self.presentControllerWithName("ModalInterfaceController", context: self)
}
}
ModalInterfaceController.swift
这是我的模态控制器的类。我持有我以前的控制器的引用( self.delegate = context为?InterfaceController
)。当选择一行时,我会在解除它之前调用我的委托函数 didSelectItem(selectedItem)
。
This is the class of my modal controller. I hold the reference of my previous controller (self.delegate = context as? InterfaceController
). When a row is selected, I call my delegate function didSelectItem(selectedItem)
before dismissing it.
protocol ModalItemChooserDelegate {
func didSelectItem(itemSelected:String)
}
class ModalInterfaceController: WKInterfaceController {
let rowId = "CustomTableRowController"
let items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
var delegate: InterfaceController?
@IBOutlet weak var customTable: WKInterfaceTable!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
self.delegate = context as? InterfaceController
// Configure interface objects here.
println(delegate)
loadTableData()
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
private func loadTableData(){
customTable.setNumberOfRows(items.count, withRowType: rowId)
for(i, itemName) in enumerate(items){
let row = customTable.rowControllerAtIndex(i) as! TableRowController
row.fillRow(itemName)
}
}
override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
let selectedItem = items[rowIndex]
self.delegate?.didSelectItem(selectedItem)
self.dismissController()
}
}
这是我将数据传回我以前的控制器的方法。如果有更好的方式让我知道,我会接受它。 :)
This is how I pass data back to my previous Controller. If is a better way let me know, I'll take it. :)
这篇关于从WatchKit中的模态视图传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!