【ERROR】Application tried to push a nil view controller on target

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 混用时,需要注意这点。

你可能感兴趣的:(【ERROR】Application tried to push a nil view controller on target)