Heim  >  Fragen und Antworten  >  Hauptteil

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中文网2727 Tage vor765

Antworte allen(1)Ich werde antworten

  • PHP中文网

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

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

    Antwort
    0
  • StornierenAntwort