Watchkit - 如何使用 contextForSegueWithIdentifier 传递多个值
Watchkit - How pass multiple values with contextForSegueWithIdentifier
我目前使用 contextForSegueWithIdentifier
将字符串值从一个 WKInterfaceController
传递到另一个,如下所示:
override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? {
if segueIdentifier == "segueDetails" {
return self.string1
}
// Return data to be accessed in ResultsController
return nil
}
然后在目的地 WKInterfaceController
我执行以下操作:
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if let val: String = context as? String {
self.label.setText(val)
} else {
self.label.setText("")
}
// Configure interface objects here.
}
但是我想传递多个值,使用两个额外的属性和字符串值 string2
和 string3
。
如何将额外的字符串值传递给 WKInterfaceController
?
Swift 具有三种集合类型:Array
、Dictionary
和 Set
.
将上下文作为字符串数组传递:
...
if segueIdentifier == "segueDetails" {
return [string1, string2, string3]
}
func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
if let strings = context as? [String] {
foo.setText(strings[0])
bar.setText(strings[1])
baz.setText(strings[2])
}
}
将上下文作为字典传递:
...
if segueIdentifier == "segueDetails" {
return ["foo" : string1, "bar" : string2, "baz" : string3]
}
func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
if let strings = context as? [String: String] {
foo.setText(strings["foo"])
bar.setText(strings["bar"])
baz.setText(strings["baz"])
}
}
就我个人而言,我更喜欢使用字典,因为如果调用者更改了字符串的顺序或数量,数组方法会更加脆弱。
无论哪种方式,添加必要的检查以使您的代码健壮。
我目前使用 contextForSegueWithIdentifier
将字符串值从一个 WKInterfaceController
传递到另一个,如下所示:
override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? {
if segueIdentifier == "segueDetails" {
return self.string1
}
// Return data to be accessed in ResultsController
return nil
}
然后在目的地 WKInterfaceController
我执行以下操作:
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if let val: String = context as? String {
self.label.setText(val)
} else {
self.label.setText("")
}
// Configure interface objects here.
}
但是我想传递多个值,使用两个额外的属性和字符串值 string2
和 string3
。
如何将额外的字符串值传递给 WKInterfaceController
?
Swift 具有三种集合类型:Array
、Dictionary
和 Set
.
将上下文作为字符串数组传递:
...
if segueIdentifier == "segueDetails" {
return [string1, string2, string3]
}
func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
if let strings = context as? [String] {
foo.setText(strings[0])
bar.setText(strings[1])
baz.setText(strings[2])
}
}
将上下文作为字典传递:
...
if segueIdentifier == "segueDetails" {
return ["foo" : string1, "bar" : string2, "baz" : string3]
}
func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
if let strings = context as? [String: String] {
foo.setText(strings["foo"])
bar.setText(strings["bar"])
baz.setText(strings["baz"])
}
}
就我个人而言,我更喜欢使用字典,因为如果调用者更改了字符串的顺序或数量,数组方法会更加脆弱。
无论哪种方式,添加必要的检查以使您的代码健壮。