1 设置根视图
2 如果是UINavigationController,用入栈和出栈实现页面跳转,调用UINavigationController方法,注意:rootview视图对UINavigationController的引用是self.navigationController
入栈方法: pushViewController:animated:
出栈方法: popRootviewControllerAnimated:
3 用模态方法实现页面跳转(如:实现选择内容),定义主视图名字MainView,模态视图名字ModalView
主视图调用模态视图:[self presentModalViewController:ModalView animated:YES]; self是主视图
模态视图关闭自己:[self dismissModalViewControllerAnimated:YES]; self是模态视图
以上presentModalViewController:animated:和dismissModalViewControllerAnimated:从iOS 6开始已经被弃用,但是编译不会出错,只是警告,使用没有问题。
在iOS 6后用以下两个方法代替上述方法
presentViewController:animated:completion:
dismissViewControllerAnimated:completion:
和以前的方法相比,多了completion:选择器,completion选择器可以理解为在实行该方法后执行的方法,注意不是像回调函数那种,
MainView执行了presentViewController:animated:completion:后,系统将以模态的方式将ModalView显示出来,同时,如果定义了Completion,系统在显示ModalView后执行改标签指定的动作,而不是在ModalView完成后执行Completion指定的动作。
同样,在ModalView中执行dismissViewControllerAnimated:completion:方法,在ModalView消失后执行Completion方法。
例如:
[self presentViewController:second animated:YES completion:^{[self animationCompleted];}];
-(void)animationCompleted{
// Whatever you want to do after finish animation
NSLog(@"Animation Completed")
}
4 弹出窗口虽然不属于页面跳转,这里一并写了,常用的有UIAlertView和UIActionSheet,实现都很简单,实例化->设置委托->show。这里要注意的时设置委托UIAlertView的委托是UIAlertViewDelegate,一般情况是:谁调用UIAlertView,谁就设置为该对象的委托。需要注意一个视图可能是多个UIAlertView的实例的委托,这是要注意区分。在委托对象中实现委托方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
即可,如果不现实的声明遵守UIAlertViewDelegate协议,只要实现了上述方法,系统也能编译通过,并且没有警告。
UIActionSheet与UIAlertView类似,这里不在叙述。
杂谈:从Delegate可以窥探出iOS在页面之间传值的一种方式。现在有视图A、B;委托D。要实现A弹出B,在B做了选择后,将选择结果返回给A。
实现原理为:A实现委托D,B包含D实例的引用(注意,这里的引用不能是strong和weak,应该为__undafe_unretained,因为不是weak,所以程序释放delete对象时,系统不是自动设为nil,需在dealloc中手动设置。),A实例化B,设置B的引用为自己,显示B,B执行选择,调用委托方法。