WatchKit中的prepareForSegue等效方法

14

我有一个带有两个按钮的界面,它们都调用相同的接口,但使用不同的信息。在传统的界面上,我使用prepareForSegue,但是我不知道WatchKit上的等效方法。

4个回答

35

你可以有两种方法来做到这一点:

在你的storyboard中,你可以在segue中设置一个标识符:

enter image description here

然后你可以使用contextForSegueWithIdentifier

- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier {
     if ([segueIdentifier isEqualToString:@"yourIdentifier"]) {
        return aDictionaryWithYourInformation;
     }
}

或者您可以通过代码传递带有上下文的信息,例如:

[self pushControllerWithName:@"YourViewController"
                     context:aDictionary];

这个上下文是一个字典,你可以在- (void)awakeWithContext:(id)context中访问它。


正确答案,类似的问答:https://dev59.com/nF8d5IYBdhLWcg3wQwa5#46421098 - BootMaker

11

在 Watchkit 中进行转场导航有两种方法可以使用,分别是 WKInterfaceController 中的两个方法:

override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
        return //your object
    }

并且适用于表格

override func contextsForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> [AnyObject]? {
      return  //your object
    }

在推送的界面控制器中,你可以通过func awakeWithContext(context: AnyObject?)方法获取传递给它的对象。


2
在WatchKit中,您可以使用此代码调用WKInterfaceController:
[self pushControllerWithName:@"YourControlName"
          context:[self contextForSegueWithIdentifier:@"YourControlName"]];

1

For tables it's the following:

override func contextForSegue(withIdentifier segueIdentifier: String, in table: WKInterfaceTable, rowIndex: Int) -> Any? {
     return  //your object
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接