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

 手机用户2602884633 发布于 2022-10-26 12:29

网上查了一些文章,找到一些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 个回答
    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; 
        }
    }
    2022-10-27 00:59 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有