美文网首页
iOS使用StroryBoard页面跳转及传值

iOS使用StroryBoard页面跳转及传值

作者: 心至靜行至遠 | 来源:发表于2016-09-06 14:18 被阅读228次

苹果官方是推荐我们将所有的UI都使用Storyboard去搭建,Storyboard也是一个很成熟的工具了。使用Storyboard去搭建所有界面,我们可以很迅捷地搭建出复杂的界面,也就是说能为我们节省大量的时间。我们还可以很直观地看出各个界面之间的关系,修改起来也很方便。将来如果遇到需要作修改的地方,我们只需要找到相对应的Storyboard就可以了,比起以前来说,快捷了不少。

接下来就来介绍一些如何使用StroryBoard页面跳转及传值。下面是使用StroryBoard实现的纯页面跳转的demo演示:


demo演示

StoryBoard页面跳转主要有三种方法:

1.通过使用StoryBoardID跳转。

2.通过Segue跳转。

3.页面跳转直接绑定到按钮的点击事件中,非常方便。

方法一:通过使用StoryBoardID跳转

首先需要设置要跳转到VC的StoryBoard ID,如图:


设置VC的StoryBoardID

实现跳转和传值代码:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CADetailViewController *detail = [storyboard instantiateViewControllerWithIdentifier:@"CADetailViewController"];
//传值方式跟我们代码跳转相同
detail.titleString = @"StoryBoardID跳转";
[self.navigationController pushViewController:detail animated:YES];

方法二:通过Segue跳转

同方法一差不多,需要配置Segue的identifier,如图:


配置Segue的identifier

实现跳转和传值代码:

[self performSegueWithIdentifier:@"DetailSegue" sender:nil];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"DetailSegue"]) {
        //传值形式
        CADetailViewController *detail = segue.destinationViewController;
        detail.titleString = @"Segue跳转";
    }
}

方法三:按住control键选中按钮同时拖线到要跳转到的视图控制器内,选中跳转方式即可。


方法三实际上也是通过 Segue跳转,会调用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender方法。

相关文章

网友评论

      本文标题:iOS使用StroryBoard页面跳转及传值

      本文链接:https://www.haomeiwen.com/subject/yqydettx.html