第02天实战技术(16):父子控制器的重要性(modal)

原创
2017/03/26 21:58
阅读数 29

#####一、父子控制器的重要性(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

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部