您的位置:首页 > 移动开发 > Objective-C

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

2017-03-26 00:00 330 查看
#####一、父子控制器的重要性(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息