navigationController的pushViewController无效问题
参考资料:http://www.cocoachina.com/ask/questions/show/52842
我觉得这个问题很有代表性,尤其是小白,所以将重点内容摘抄过来了。
问题及操作过程:
1、新建一个 single View Application,会自动给我生成一个带xib的文件,就是默认的起始界面。 然后在xib里面加一个btn,以及触发事件onClick
2、新建一个 UIViewController,叫做pageFirst。
3、在默认的起始界面的onClick方法里面写:
- (IBAction)onClick:(id)sender
{
pageFirst *firstview = [[pageFirst alloc] initWithNibName:@"pageFirst" bundle:nil];
[self.navigationController pushViewController:firstview animated:YES];
}
界面没有发生变化,但是程序确实被运行了,也没有报错,然后论坛也有好多人问过,但是都没有解决我这个问题
原因:
navigationController是假的,并没有实例化,只是放在了view上而已。需要实例化或者关联起来。
实例化如下:
UINavigationController *nav = [UINavigationController alloc]initWithRootController:....]
这个函数去试一下,然后presentModalViewController