SWViewController *swVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SWView"];
[self.navigationController pushViewController:swVC animated:YES];
上面的代碼進(jìn)行頁(yè)面跳轉(zhuǎn)時(shí),報(bào)錯(cuò)
Application tried to push a nil view controller on target UINavigationController
這個(gè)錯(cuò)誤的原因是self.storyboard
等于 nil债朵,self
表示的 VC 是由代碼生成的,在 storyboard 中沒(méi)有相應(yīng)的視圖,自然無(wú)法查找到@"SWView"
吏颖,SWViewController
則是在 storyboard 中直接生產(chǎn)的。要解決這個(gè)錯(cuò)誤恨樟,一可以將self
表示的 VC 在 storyboard 中生產(chǎn)對(duì)應(yīng)的視圖半醉,二是可以使用下面代碼來(lái)查找 storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
SWViewController *swVC = [storyboard instantiateViewControllerWithIdentifier:@"SWView"];
[self.navigationController pushViewController:swVC animated:YES];
storyboard 生成 VC 、代碼生成 VC 混用時(shí)劝术,需要注意這點(diǎn)缩多。