首頁  >  問答  >  主體

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 天前769

全部回覆(1)我來回復

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

    回覆
    0
  • 取消回覆