iOS UIViewController中的parentView

2019-11-15  本文已影响0人  喔牛慢慢爬

1、@property(nullable,nonatomic,weak,readonly) UIViewController *parentViewController;

当前控制器的父视图控制器

2、@property(nullable, nonatomic,readonly) UIViewController *presentedViewController;

被当前视图控制器present出来的视图控制器的实例对象;

3、@property(nullable, nonatomic,readonly) UIViewController *presentingViewController;

将当前视图控制器present出来的视图控制器的实例对象;

#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

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)

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)

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)


上一篇下一篇

猜你喜欢

热点阅读