putting ivars into init

只愿长相守 提交于 2019-11-28 14:44:47

You're using a storyboard segue. The segue will create the destination view controller by loading it from the storyboard. There's no point in creating a -[BSotherViewController initWithNumber:array:] method, because the segue won't use it.

When the user triggers the segue by tapping the button, the system creates an instance of UIStoryboardSegue. This segue object has a destinationViewController property which will (in your case) be the BSotherViewController instance that is about to appear. The system sends a prepareForSegue:sender: message to the source view controller (your BSViewController), and passes the segue object as the first argument.

You need to implement prepareForSegue:sender: in BSViewController. In that method, you have access to both the source view controller (self) and the destination view controller (segue.destinationViewController), so you can pass data from one to the other.

First, add number and array properties to BSotherViewController. Then, add a method like this to BSViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // The identifier here must match the one you assigned to the segue in the storyboard.
    if ([segue.identifier isEqualToString:@"GoingToOtherViewController"]) {
        BSotherViewController *destination = segue.destinationViewController;
        destination.number = self.number;
        destination.array = self.array;
    }
}

Finally, in -[BSotherViewController viewDidLoad], use the values of your new number and array properties to set the content of your views.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!