最近做一個練習項目,在做用戶引導頁的時候需要在播放完引導頁之后跳轉到navigationViewController
實現代碼如下:
//1,首先獲得當前工程的storyboard文件,方法如下:
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//2,拿到帶有指定標簽的viewcontroller:
這個需要在storyboard中設置一下將要跳轉到的頁面的id,在storyboard可以找到這個設置,填一個id即可
LCHMyNavigationController *nvc = [story instantiateViewControllerWithIdentifier:@"LCHNAVCONTROLLER"];
//3,最后做一下跳轉即可:
[self presentViewController:nvc animated:YES completion:nil];
這樣跳過去的頁面,會完整保留storyboard種定義的外觀以及被彈出頁面與其他storyboard中頁面的跳轉關系,
還有對應的xib文件中定義的空間布局,不會丟失任何信息,但如果是用代碼new出來或者alloc一個想要跳轉過去的ViewController,
然后再push出來,那么彈出來的頁面就會丟失xib文件的布局和storyboard定義的頁面關系。完全是一個新的頁面實例,
不帶有任何storyboard和xib的內容,無法實現想要的效果。
最關鍵的是由于UINavigationController是繼承自UIViewController的,
所以這個方法還可以跳轉至storyboard中帶有指定id的UINavigationController上,
這樣就可以實現從一個UINavigationController的某個界面跳至另外一個UINavigationController的根頁面上,
也就是可以在兩個storyboard中擺放的UINavigationController之間進行跳轉。
- 附:其他跳轉到storyboard和xib的方式
//1,跳轉到xib 假設有一個按鈕,這個按鈕就是實現跳轉的,那么在這個按鈕的點擊事件中,代碼可以這樣寫。
AViewController *a1= [[AViewController alloc]initWithNibName:@”AViewController” bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:a1 animated:YES];
//2,跳轉到storyboard 如上,代碼可以這樣寫
UIStoryboard *sb=[UIStoryboard storyboardWithName:@”A” bundle:nil];
[self presentViewController:[sb instantiateInitialViewController] animated:YES completion:nil];