search

Home  >  Q&A  >  body text

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怎么跳转怎么传值?谢谢!

PHP中文网PHP中文网2757 days ago812

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-02 09:31:33

    1. Is your A placed inside the navigationviewcontroller? Then there is no need to change the last sentence

    2. If it is not in navgationviewcontroller, it depends on how you want to display your B. For example, if it is present, you can [self presentViewController:B animated:YES completion:nil]

    3. If you want to use [self performSegueWithIdentifier:@"c" sender:nil]; this way, you don’t need to get the B viewcontroller in the action. The code is modified as follows

    - (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; 
        }
    }

    reply
    0
  • Cancelreply