objective-c - storyboard页面跳转和传值

查看:331
本文介绍了objective-c - storyboard页面跳转和传值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

网上查了一些文章,找到一些storyboard页面跳转和传值的方法,试了一下,有一些有用,有的没用,现归纳如下(页面A跳转到页面B),
跳转的方法:
1、A中有button等控件,直接按住control键拖拽到B,点击button可实现跳转;
2、按住control直接将A和B链接,选中连线添加Identifier(命名为c吧)。为A中的button创建action,action里实现方法[self performSegueWithIdentifier:@"c" sender:nil];可实现跳转

传值方法:
1、在prepareForSegue里面实现,根据连线Identifier。传值成功
2、在action里面实现,通过storyboard id找到要跳转的页面,实现跳转和传值UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ReceiveViewController *B = [storyboard instantiateViewControllerWithIdentifier:@"IdReceive"];
B.name = @"GC";
B.age = 10;
[self.navigationController pushViewController:receive animated:YES];

这种方法传值没有成功,页面也不能跳转,因为A是viewcontroller不是navigationController,所以我将最后一句改成[self performSegueWithIdentifier:@"c" sender:nil];跳转可以了,但是传值失败。

请教各位高手,通过storyboard id怎么跳转怎么传值?谢谢!

解决方案

  1. 你的 A 是放到 navigationviewcontroller 里面的吗? 那么最后一句就不用改了

  2. 如果不是在 navgationviewcontroller 里面 ,就看你想怎么展示你的 B 了, 比如 present 的话, 可以 [self presentViewController:B animated:YES completion:nil]

  3. 如果你要用[self performSegueWithIdentifier:@"c" sender:nil]; 这种方式, 就不用在 action 里面获取 B viewcontroller 了, 代码修改如下

- (void)someButtonClicked:(id)sender {
  [self performSegueWithIdentifier:@"c" sender:nil]
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender {
 if([segue.identifier isEqualToString:@"c"]){
    ReceiveViewController *B = segue.destinationViewController;
    B.name = @"GC"; 
    B.age = 10; 
    }
}

这篇关于objective-c - storyboard页面跳转和传值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆