通過「奇怪」,我注意到以下行爲:添加ChildViewController引起奇怪的行爲對孩子的ViewController
- 背景顏色由
ChildViewController
設置不會出現 - 儘管增加了基本的點觸手勢識別到
ChildViewController
,沒有抽頭識別 - 添加
UIButton
到示於UIButton
的ChildViewController的結果,但存在對的UIButton部分攻絲時沒有響應
在我ParentViewController
,目前我ChildViewController
很一般,像這樣:
UIViewController *viewController;
viewController = (ChildViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
viewController.view.translatesAutoresizingMaskIntoConstraints = NO;
viewController.view.center = self.view.center;
[self.view addSubview:viewController.view];
[self.view bringSubviewToFront:viewController.view];
[viewController didMoveToParentViewController:self];
這裏有一些很基本的東西我的viewDidLoad
ChildViewController
做:
self.view.backgroundColor = [UIColor yellowColor];
UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeSystem];
[tapButton addTarget:self action:@selector(tappedOnTap) forControlEvents:UIControlEventTouchUpInside];
//button view customization code omitted for brevity
tapButton.userInteractionEnabled = YES;
tapButton.enabled = YES;
[self.view addSubview:tapButton];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOnView)];
[self.view addGestureRecognizer:tapGesture];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];
我保證一切都正確連接 - 但是,當我在模擬器或設備上點擊它時,tappedOnView
和tappedOnTap
中沒有確認。
我缺少一些基本的東西介紹ChildViewController
?
編輯
對於那些好奇,這個非常基本的應用程序是Github。
也許嘗試移動它了'viewDidLoad'改爲'viewDidAppear',看看會發生什麼 – random
奇怪的是,將它移動到'viewDidAppear'會導致一切消失在一起。 – daspianist
嘗試刪除此行[viewController didMoveToParentViewController:self];並做到這一點[自我addChildViewController:viewController]; [self.view addSubview:tapButton]後; –