SWViewController *swVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SWView"];
[self.navigationController pushViewController:swVC animated:YES];
上面的代碼進行頁面跳轉時,報錯
Application tried to push a nil view controller on target UINavigationController
這個錯誤的原因是self.storyboard
等于 nil,self
表示的 VC 是由代碼生成的,在 storyboard 中沒有相應的視圖,自然無法查找到@"SWView"
,SWViewController
則是在 storyboard 中直接生產的。要解決這個錯誤,一可以將self
表示的 VC 在 storyboard 中生產對應的視圖,二是可以使用下面代碼來查找 storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
SWViewController *swVC = [storyboard instantiateViewControllerWithIdentifier:@"SWView"];
[self.navigationController pushViewController:swVC animated:YES];
storyboard 生成 VC 、代碼生成 VC 混用時,需要注意這點。