獲取 storyboard 上的 Controller

有時候在使用 storyboard 開發的時候會碰到傳值等,這時要獲取某個跳轉的 Controller, 然後使用屬性傳值, 所以需要獲取某個Controller,不能從新 alloc,因爲從新alloc後的就不是要傳值的那個了 
下面,使用幾行可以獲取某個Controller
//點擊button跳轉
- (IBAction)buttonAction:(id)sender {

// 1.先創建 Storyboard 對象
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

//  2.獲取 Storyboard 上的VC前,此Controller先設置Identifier,有了Identifier才能獲取到想要的Controller.
    ThreeViewController *threeVC = (ThreeViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ThreeViewController"];
    //獲取到後就可以賦值了
    threeVC.name = @"劉德華";
    // 跳轉
    [self.navigationController pushViewController:threeVC animated:YES];


}
發佈了47 篇原創文章 · 獲贊 18 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章