- UIViewController中有三个属性
parentViewController、presentedViewController
和presentingViewControlle
,下面简单的介绍这三个属性的意思。 - 开发过程有个需求需要判断当前ViewController是通过push显示还是通过present显示的,所以用到了
parentViewController、presentedViewController
和presentingViewControlle
这几个属性。
1、@property(nullable,nonatomic,weak,readonly) UIViewController *parentViewController;
当前控制器的父视图控制器
2、@property(nullable, nonatomic,readonly) UIViewController *presentedViewController;
被当前视图控制器present出来的视图控制器的实例对象;
3、@property(nullable, nonatomic,readonly) UIViewController *presentingViewController;
将当前视图控制器present出来的视图控制器的实例对象;
- 代码演示:
FirstViewController.h
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"FirstVC self.presentedViewController:%@",self.presentedViewController);
NSLog(@"FirstVC self.presentingViewController:%@",self.presentingViewController);
NSLog(@"FirstVC self.parentViewController:%@",self.parentViewController);
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"FirstVC viewWillDisappear self.presentedViewController:%@",self.presentedViewController);
NSLog(@"FirstVC viewWillDisappear self.presentingViewController:%@",self.presentingViewController);
NSLog(@"FirstVC viewWillDisappear self.parentViewController:%@",self.parentViewController);
}
- (IBAction)showPresentVC:(id)sender {
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:secondVC animated:YES completion:^{
}];
}
@end
SecondViewController.h
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor yellowColor]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 100, 60)];
[button setCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2)];
[button addTarget:self action:@selector(dismissVC:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor redColor]];
[button setTitle:@"Dismiss" forState:UIControlStateNormal];
[self.view addSubview:button];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"SecondVC self.presentedViewController:%@",self.presentedViewController);
NSLog(@"SecondVC self.presentingViewController:%@",self.presentingViewController);
NSLog(@"SecondVC self.parentViewController:%@",self.parentViewController);
}
- (void)dismissVC:(id)sender
{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
@end
- 首次进入FirstVC,viewDidAppear中打印log:
2019-11-15 16:55:37.748891+0800 PresentVCDemo[3887:2431582] FirstVC viewDidAppear self.presentedViewController:(null)
2019-11-15 16:55:37.748966+0800 PresentVCDemo[3887:2431582] FirstVC viewDidAppear self.presentingViewController:(null)
2019-11-15 16:55:37.748998+0800 PresentVCDemo[3887:2431582] FirstVC viewDidAppear self.parentViewController:(null)
- 点击按钮present出SecondVC,FirstVC的viewWillDisappear中打印log:
2019-11-15 16:56:49.393651+0800 PresentVCDemo[3887:2431582] FirstVC viewWillDisappear self.presentedViewController:<SecondViewController: 0x12de15210>
2019-11-15 16:56:49.394123+0800 PresentVCDemo[3887:2431582] FirstVC viewWillDisappear self.presentingViewController:(null)
2019-11-15 16:56:49.394288+0800 PresentVCDemo[3887:2431582] FirstVC viewWillDisappear self.parentViewController:(null)
- 被present出SecondVC,viewDidAppear中打印log:
2019-11-15 16:47:57.997346+0800 PresentVCDemo[3884:2429470] SecondVC self.presentedViewController:(null)
2019-11-15 16:47:57.997882+0800 PresentVCDemo[3884:2429470] SecondVC self.presentingViewController:<FirstViewController: 0x1030080c0>
2019-11-15 16:47:57.998180+0800 PresentVCDemo[3884:2429470] SecondVC self.parentViewController:(null)
网友评论