storyboard presentViewController pushViewController 跳轉後黑屏 NavigationBar按鈕push
環境:XCode5.1,iOS6樣式,Storeboard
先在主頁面建一個navigation view,然後在導航欄放了一個button,也稱之為bar button item.
新建一個view controller(簡稱viewB),結果按住ctrl,拖動button到viewB可以建立Modal或者Push,執行就是沒反應!
好吧,那就建立一個button的action,我習慣用VC++的命名:
- (IBAction)onBtnShowModal2:(id)sender {
}這樣模擬時可以發現按鈕事件是呼叫的,那就可以用程式碼來跳轉到ViewB啦。實現之:
//導航欄按鈕彈出模態窗體 - (IBAction)onBtnShowModal2:(id)sender { //用pushviewcontroller可以出現導航欄,自動有返回按鈕,黑屏 RightBtnViewController *view = [[RightBtnViewController alloc] init]; [self.navigationController pushViewController:view animated:YES]; }
結果發現,導航欄出現了,但是介面黑屏了!使用
[self presentViewController:view animated:YES completion:nil];
結果全部黑屏
好了,換一種方法:
點選ViewB的ViewController,然後右邊identity inspector設定Storyboard ID,例如“RightWin”
//導航欄按鈕彈出模態窗體 - (IBAction)onBtnShowModal2:(id)sender { RightBtnViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"RightWin"]; [self.navigationController pushViewController:view animated:YES]; }
這下OK了,就是建立view時的不一樣!
使用[self presentViewController:view animated:YES completion:nil];也OK了,不過是全屏不帶navigation bar的。可以使用
[self dismissViewControllerAnimated:YES completion:nil];返回!!!
//============華麗的分割線==============
其實上面說了那麼多,無非證明一件事:拖動過去的button是可以用的,如果想實現導航欄按鈕的導航功能,那這個做法就很傻逼了!正確做法如下:
把button換成bar button item(在控制元件的最下面),這樣按住ctrl拖動,選擇push就OK啦,一句話也不用寫,是不是很傻逼?哈哈