iOS:Custom UIViewController Containment中的按钮事件不起作用

时间:2013-03-28 22:17:05

标签: ios button uiviewcontroller containment

我正在实现自定义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永远不会触发

1 个答案:

答案 0 :(得分:0)

我预感到您的触摸事件不会进入您的子根级视图控制器,因为您没有在自定义容器中正确使用它们。在didFinishLaunchingWithOptions:完成后,您的ViewController指针也可能不会被保留。没有提供更多代码就没有办法知道。

[编辑] 我根据我通过评论发现的内容澄清了我的答案。

  1. 我/我们无法在没有更多代码的情况下帮助原始海报,并且更深入地了解应用程序的编写,编译和运行方式。以下评论已经概述了所提出的一些问题。
  2. 目前尚不清楚如何生成AppDelegate代码,因为原始海报提到他/她不能完整地发布它并且它以某种方式自动生成。
  3. 提供的代码中有变量,其中未提供指针声明。它们是:childVCparentTopVCInnerframeLevel1btnInViewbtnInView完成后可能不会保留didFinishLaunchingWithOptions:,这可能是问题的一部分。
  4. 提供的源代码似乎不必要地调用didMoveToParentViewController:。这里没有UIViewController子类,因此不需要调用它。尝试删除它。同样,目前还不清楚如何创建或编写AppDelegate代码,或者原始海报是否真的可以这样做。
  5. 终于......

    如果你想要一个自定义视图控制器容器类,我会考虑阅读Apple的View Controller Programming Guide:

    http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457

    我肯定会阅读左侧标题为创建自定义容器视图控制器的部分,以及标题为实现自定义容器视图控制器的子部分