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

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

@property(nullable, nonatomic,readonly) UIViewController *presentedViewController  NS_AVAILABLE_IOS(5_0);
//和上面的方法刚好相反,比如,A和B两个controller,A跳转到B,那么B的presentingViewController就是A
@property(nullable, nonatomic,readonly) UIViewController *presentingViewController NS_AVAILABLE_IOS(5_0);
了解了上面方法我们可以知道,对于反向传值这样的问题,我们根本不需要代理,block,通知等这样的复杂手段,只需要获取跳转到它的Controller,直接设置即可。举个例子,我们需要在第二个界面消失后,改变第一个界面的颜色,在第二个controller中只需要下面的代码即可实现 :
    self.presentingViewController.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    [self dismissViewControllerAnimated:YES completion:nil];

六、UIViewController的模态跳转及动画特效
单纯的UIViewController中,我们使用最多的是如下的两个方法,一个向前跳转,一个向后返回:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);

从方法中,我们可以看到,有animated这个参数,来选择是否有动画特效,默认的动画特效是像抽屉一样从手机屏幕的下方向上弹起,当然,这个效果我们可以进行设置,UIViewController有如下一个属性来设置动画特效:
@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle NS_AVAILABLE_IOS(3_0);
注意,这个要设置的是将要跳转到的controller,枚举如下:
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,//默认的,从下向上覆盖
    UIModalTransitionStyleFlipHorizontal ,//水平翻转
    UIModalTransitionStyleCrossDissolve,//溶解
    UIModalTransitionStylePartialCurl ,从下向上翻页
};

除了跳转的效果,还有一个属性可以设置弹出的controler的填充效果,但是这个属性只在pad上有效,在iphone上无效,都是填充到整个屏幕:
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle NS_AVAILABLE_IOS(3_2);
//枚举如下
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
        UIModalPresentationFullScreen = 0,//填充整个屏幕
        UIModalPresentationPageSheet,//留下状态栏
        UIModalPresentationFormSheet,//四周留下变暗的空白