第02天实战技术(16):父子控制器的重要性(modal)
2017-03-26 00:00
330 查看
#####一、父子控制器的重要性(modal)
code
ViewController
ModalViewController
ChildViewController
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
相关文章推荐
- 第02天实战技术(15):父子控制器的重要性(push)
- 第02天实战技术(14):父子控制器(完善)
- 第02天实战技术(13):父子控制器(简介)
- 第02天实战技术(17):父子控制器(实现)
- tensorflow55 《TensorFlow技术解析与实战》16 TensorFlow Debugger
- 第02天实战技术(06):项目恶心Bug
- 第02天实战技术(12):extern和const联合使用
- 项目实战No7 标签按钮 父子控制器
- 第02天实战技术(05):super,superClass,class
- 第02天实战技术(11):static和const联合使用
- 第04天实战技术(16):UICollectionView定位
- 第02天实战技术(04):runtime字典转模型
- 第02天实战技术(10):static和extern使用
- iOS_16_开关控制器_modal_代码方法
- iOS_16_控制器切换_modal_storyboard拖线的方式
- iOS_16_控制器切换_modal_storyboard拖线的方式
- 第02天实战技术(03):Runtime(字典转模型,一级转换)
- 第02天实战技术(09):开发中const使用场景
- iOS_16_控制器切换_modal_代码方式
- 第02天实战技术(02):KVC字典转模型