iOS App开发中UIViewController类的使用教程

2020-01-15 17:07:08丽君

        UIModalPresentationCurrentContext ,//和跳转到它的控制器保持一致
        UIModalPresentationCustom NS_ENUM_AVAILABLE_IOS(7_0),//自定义
        UIModalPresentationOverFullScreen NS_ENUM_AVAILABLE_IOS(8_0),
        UIModalPresentationOverCurrentContext NS_ENUM_AVAILABLE_IOS(8_0),
        UIModalPresentationPopover NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED,
        UIModalPresentationNone NS_ENUM_AVAILABLE_IOS(7_0) = -1,        
};

 

七、viewController显示(关闭)其它viewController
在5.0之前,对应的方法是使用model view controller系列的方法。5.0以后增加了presented,prensentingViewController的概念,modalViewController对应5.0后的presentedViewController

FCDemoViewController *controller= [[FCDemoViewController alloc]initWithNibName:@"FCDemoViewController" bundle:nil]; 
controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:controller animated:YES]; 
NSLog(@"%@",self.modalViewController); 
//5.0  
[self presentViewController:controller animated:YES completion:nil]; 
NSLog(@"%@",self.presentedViewController); 
NSLog(@"%@",self.presentingViewController);  

 属性modalPresentationStyle在iPad下有几种显示形式,对应不同的显示区域。属性modellTransitionStyle决定过渡形式.
关闭也有对应的方法:dismissModalViewControllerAnimated或iOS5.0以后的dismissViewControllerAnimated:completion:
5.0以后其它的变化:
controller可以自定义子controller的集合,这样每一个controller都可以是一个container controller.
definesPresentationContext决定当前显示其它controller的controller是否提供context给presentedViewController(modalViewController),如果不,就会找上一级的controller的该值。
详细的操作可以查看class reference.
苹果官方推荐使用协议的方式来让controller相互通信。首先声明一个协议,并在主controller中实现该协议的方法,在显示其它controller的时候,为其设置delegate=self.这样在其它controller需要回调presentingViewController就可以直接用delegate方法来回调到它。通过这样的方式,可以使得复用性大大增强。而且被显示的controller也不用完全知道显示它的controller的所有信息和属性。


注:相关教程知识阅读请移步到IOS开发频道。