我正在实现自定义UIViewController Containment。
此外,我还在为子视图控制器的视图添加按钮 在层次结构中。
问题是按钮事件永远不会触发按钮 在层次结构中的子视图控制器中。他们工作 只有放置在根级别以下的一个级别 - parentTopVc。
架构是这样的:
根位于:self.window.rootViewController = rootVc
层次结构:
0 ----- rootVc
1 ----------- parentTopVc< ---按钮事件DO fire
2 ---------------------- firstTopVc< ---按钮事件不会触发
2 ---------------------- secondTopVc< ---按钮事件不会触发
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *rootVc;
UIViewController *parentTopVc;
UIViewController *firstTopVc;
UIViewController *secondTopVc;
rootVc = [UIViewController new];
self.window.rootViewController = rootVc;
parentTopVc = [UIViewController new];
[rootVc addChildViewController:parentTopVc];
[parentTopVc didMoveToParentViewController:rootVc];
//
parentTopVc.view.translatesAutoresizingMaskIntoConstraints = NO;
[rootVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:parentTopVc.view attribute:NSLayoutAttributeCenterX
relatedBy:0
toItem:rootVc.view attribute:NSLayoutAttributeCenterX
multiplier:1 constant:0]];
[rootVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:parentTopVc.view attribute:NSLayoutAttributeTop
relatedBy:0
toItem:rootVc.view attribute:NSLayoutAttributeTop
multiplier:1 constant:50]];
[rootVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:parentTopVc.view attribute:NSLayoutAttributeLeft
relatedBy:0
toItem:rootVc.view attribute:NSLayoutAttributeLeft
multiplier:1 constant:0]];
[rootVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:parentTopVc.view attribute:NSLayoutAttributeRight
relatedBy:0
toItem:rootVc.view attribute:NSLayoutAttributeRight
multiplier:1 constant:0]];
[parentTopVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:parentTopVc.view attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:nil attribute:0
multiplier:1 constant:200]];
[rootVc.view addSubview: parentTopVc.view];
parentTopVc.view.backgroundColor = [UIColor clearColor];
firstTopVc = [UIViewController new];
secondTopVc = [UIViewController new];
[parentTopVc addChildViewController:childVc];
UIView* childVcInnerFrame = [[UIView alloc] initWithFrame:CGRectMake(xc,
yc,
wc,
hc)]; //x, y, w, h
[childVc.view addSubview: childVcInnerFrame];
firstTopVc.view.translatesAutoresizingMaskIntoConstraints = NO;
[parentTopVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:firstTopVc.view attribute:NSLayoutAttributeCenterX
relatedBy:0
toItem:parentTopVc.view attribute:NSLayoutAttributeCenterX
multiplier:1 constant:0]];
[parentTopVc.view addConstraint:
[NSLayoutConstraint
constraintWithItem:firstTopVc.view attribute:NSLayoutAttributeTop
relatedBy:0
toItem:parentTopVc.view attribute:NSLayoutAttributeTop
multiplier:1 constant:0]];
// activate
[parentTopVc.view addSubview: firstTopVc.view];
[firstTopVc didMoveToParentViewController:parentTopVc];
UIView *parentTopVcBtnViewsLevel1frame = [UIView new];
// add view to root
[parentTopVcInnerframeLevel1 addSubview: parentTopVcBtnViewsLevel1frame];
UIView *vtemp1;
vtemp1 = [UIView new];
[parentTopVcBtnViewsLevel1frame addSubview: vtemp1];
vtemp1.translatesAutoresizingMaskIntoConstraints = NO;
[parentTopVcBtnViewsLevel1frame addConstraint:
[NSLayoutConstraint
constraintWithItem:vtemp1 attribute:NSLayoutAttributeLeft
relatedBy:0
toItem:parentTopVcBtnViewsLevel1frame attribute:NSLayoutAttributeLeft
multiplier:1 constant:10]];
[parentTopVcBtnViewsLevel1frame addConstraint:
[NSLayoutConstraint
constraintWithItem:vtemp1 attribute:NSLayoutAttributeTop
relatedBy:0
toItem:parentTopVcBtnViewsLevel1frame attribute:NSLayoutAttributeTop
multiplier:1 constant:10]];
//v1: W
[vtemp1 addConstraint:
[NSLayoutConstraint
constraintWithItem:vtemp1 attribute:NSLayoutAttributeWidth
relatedBy:0
toItem:nil attribute:0
multiplier:1 constant:w]];
//v1: H
[vtemp1 addConstraint:
[NSLayoutConstraint
constraintWithItem:vtemp1 attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:nil attribute:0
multiplier:1 constant:h]];
// button
btnInView = [UIButton buttonWithType:UIButtonTypeCustom]; //
[btnInView setBackgroundColor:[UIColor clearColor]];
btnInView.showsTouchWhenHighlighted=YES;
[btnInView setFrame: CGRectMake(0,0,w,h)]; // set up frame
[vtemp1 addSubview:btnInView]; // place button in view
[btnInView addTarget:self action:@selector(doShowNextTopVc:)
forControlEvents:UIControlEventTouchUpInside];
}
//
-(void) doShowNextTopVc:(UIButton *)paramSender{
NSLog(@"doShowNextTopVc: %@", @"START <==");
}
doShowNextTopVc永远不会触发
答案 0 :(得分:0)
我预感到您的触摸事件不会进入您的子根级视图控制器,因为您没有在自定义容器中正确使用它们。在didFinishLaunchingWithOptions:
完成后,您的ViewController指针也可能不会被保留。没有提供更多代码就没有办法知道。
[编辑] 我根据我通过评论发现的内容澄清了我的答案。
childVC
,parentTopVCInnerframeLevel1
和btnInView
。 btnInView
完成后可能不会保留didFinishLaunchingWithOptions:
,这可能是问题的一部分。didMoveToParentViewController:
。这里没有UIViewController子类,因此不需要调用它。尝试删除它。同样,目前还不清楚如何创建或编写AppDelegate代码,或者原始海报是否真的可以这样做。终于......
如果你想要一个自定义视图控制器容器类,我会考虑阅读Apple的View Controller Programming Guide:
我肯定会阅读左侧标题为创建自定义容器视图控制器的部分,以及标题为实现自定义容器视图控制器的子部分