#####一、父子控制器的重要性(modal)
modal出来的控制器(父控制器)
那么父控制器添加的子控制器都需要
[self addChildViewController:子控制器];
只有这样,那么它们才回成为父子关系
>>
否则
在子控制器里面 不能够disMiss操作
code ViewController
#import "ViewController.h"
#import "ModalViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ModalViewController *vc = [[ModalViewController alloc]init];
vc.view.backgroundColor = [UIColor redColor];
[self presentViewController:vc animated:YES completion:nil];
}
@end
ModalViewController
#import "ModalViewController.h"
#import "ChildViewController.h"
@interface ModalViewController ()
@property(nonatomic) ChildViewController *cVC;
@end
@implementation ModalViewController
- (void)viewDidLoad {
[super viewDidLoad];
ChildViewController *cVC = [[ChildViewController alloc]init];
cVC.view.backgroundColor = [UIColor grayColor];
cVC.view.frame = CGRectMake(50, 50, 200, 200);
[self.view addSubview:cVC.view];
[self addChildViewController:cVC]; // 让当前控制器 和 ChildViewController(子) 成为父子关系
//_cVC = cVC;
}
@end
ChildViewController
#import "ChildViewController.h"
@interface ChildViewController ()
@end
@implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"点击了子控制器的view");
// 判断当前方法调用者是不是被modal出来的, 如果不是,判断父控制器是不是被modal出来
[self dismissViewControllerAnimated:YES completion:nil];
}
@end