一、介绍
之前实现了TabBarController和TableViewController,即相当于完成了登录后的主页面,现在需要插入一个View,用于登录注册,且这个页面应该出现在主页面之前。
二、实现
这里主要是用到NavigationController来实现,因为NavagationController是一个存放ViewController栈,在AppDelegate.m文件中,设置登录后主页面为NavigationController的根页面,同时将需要插入的登录注册页面push进栈中即可,主要代码如下:
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:rootTabBarView];
self.window.rootViewController = rootNav;
CCLoginViewController *ccLoginView = [[CCLoginViewController alloc] init];
[rootNav pushViewController:ccLoginView animated:NO];
三、微小的工作
(1)pushViewController和presentViewController的作用都是插入一个新的View,但是pushViewController是在当前的Navigation栈中插入,而present是另起新的一个页面。
(2)在push或者present新页面的时候,如果新页面的背景颜色没有设定会产生卡顿的感觉,其实是动画过渡的时候新页面透明导致的。
(3)如果用pushViewController插入新页面(同一个NavigationController下),且想返回之前的某个页面,可以用popToViewController,使用方法如下:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2]
animated:YES];
for (UIViewController *temp in self.navigationController.viewControllers) {
if ([temp isKindOfClass:[SomeViewController class]]) {
[self.navigationController popToViewController:temp animated:YES];
}
}