UINavigation,UiView,ModalView Controller之间的关系

如果当前是个VC,那么就太简单了,直接就可以push到下一个vc
AddShopViewController *controller = [[AddShopViewController alloc] init];
controller.view.backgroundColor = [UIColor whiteColor];// 设置背景色为白色,消除残影
[self.navigationController pushViewController:controller animated:YES];
有时候需要从modal view里pushviewcontroller,这当然是不行的,因为你没有NC,可以NSLog一下试试就知道了。
NSLog(@"ViewControllers before pushing: %@", self.navigationController.viewControllers);
这时你要加某个viewController的view在你的self.view上,你必须做的是:[self addChildViewController:xxx];或者addSubview。因为没有nav,如果想用nav来控制vc,一般是先有nav,然后把vc加入nav中,如果本身就是个vc那么直接
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self];
[self presentViewController:navController animated:YES completion:nil];
这样你的modal就有一个nv,那么你就可以在这个modal里边用[self.navigationController pushViewController:controller animated:YES];
完整的一个写法是这样的,Storyboard表述的很清楚
MyViewController *myViewController = [MyViewController alloc] init];
UINavigationController *navController = [UINavigationController alloc] initWithRootViewController:myViewController];
// Presuming a view controller is asking for the modal transition in the first place.
[self presentViewController:navController animated:YES completion:nil];
posted @ 2015-02-24 21:21  xxd  阅读(2351)  评论(0编辑  收藏  举报