如题,添加addChildViewController子viewController仍然无响应
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 200, 60, 60)];
[btn setTitle:@"点我" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:btn];
[self addChildViewController:self.imgVC];
}
- (void)btnClick{
NSLog(@"点击了按钮");
}
// 懒加载
- (addImgVC *)imgVC{
if (!_imgVC) {
_imgVC = [[addImgVC alloc]init];
[_imgVC.view setFrame:CGRectMake(0, 50, 375, 62)];
[self.view addSubview:_imgVC.view];
}
return _imgVC;
}
你的调用流程还是没弄清楚,把你的
[self.view addSubview:self.imgVC.view]
写在addChildViewController
就可以了。因为你懒加载的时候,你写的流程是先添加了view
,然后再添加子控制器,然而正确的流程是先添加子控制器,然后才能添加view