情景再現
1,自定義一個storyboard:
打開xcode,按下cmd+N,新建一個Storyboard--->next
將新建立的storyboard命名為:TestViewController--->create
在TestViewController.storyboard上加上一個label,其text為:Hello-ios
2,編寫UIButton的UIControlEventTouchUpinside事件:
- (IBAction)btnClick:(id)sender { //創建加載storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TestViewController" bundle:nil]; //將控制器關聯到storyboard
_viewOfStoryboard = [storyboard instantiateInitialViewController]; _viewOfStoryboard.view.backgroundColor = [UIColor whiteColor]; [self dismissViewControllerAnimated:YES completion:nil]; [self presentViewController:_viewOfStoryboard animated:YES completion:nil]; }
運行操作如下:
在加載自定義storyboard界面遇到如下問題
錯誤原因:應用程序視圖向目標展示一個空模式形態的視圖控制器,簡單的說,storyboard中的視圖控制器是空的。
3,解決方案:
- 在StackOverflow上給了相關回答(下面是個人的翻譯,如有誤請指出)
Check the identifier of the viewcontroller, if it is the same that you mentioned in storyboard。
- 檢查viewcontroller上的標識符,確保它在storyboard上是獨一無二的。
Make sure that your buddiesOrFacebook is not nil. Set a breakpoint on that line and on the debug area at the bottom see whether the object is not nil. If it is nil then problem lies in the storyboard connection。
- 確保你的buddies或者facebook(這句話沒有理解)不是空的。可以設置斷點進行調試,如果該對象是空的,那么就是storyboard的連接出現了問題。
If your current viewcontroller is not launched from storyboard then get storyboard object like this :
如果當前的viewcontroller沒有開始加載,可以參考下面的標准寫法:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController * buddiesOrFacebook = [storyboard instantiateViewControllerWithIdentifier:@"BuddiesFBFriends"] ; [self presentViewController:buddiesOrFacebook animated:YES completion:nil];
上面的解決方案並沒有解決我的問題,於是采取了第二套解決方案。
打開TestViewController.storyboard視圖,操作如下:
選中Is Initial View Controller,確保viewcontroller被初始化。
4,結果:
最后運行正確,如下圖: