我實現一個自定義的UIViewController遏制不起作用。的iOS:在自定義的UIViewController遏制按鈕事件
而且我添加按鈕子視圖控制器的意見 下來的層次。
的問題是,當按鈕 在子視圖控制器向下層次按鈕事件永遠不會觸發。只有放在根級別以下的一個級別 - parentTopVc,它們才能工作 。
架構是這樣的:
根爲:self.window.rootViewController = rootVc
層次結構:
0 ----- rootVc
1 --- -------- parentTopVc < ---按鈕事件DO消防
2 ---------------------- firstTopVc < --- button事件不會火
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永遠不會觸發
謝謝。我閱讀了上述鏈接和其他信息。 沒有任何運行時間警告,代碼正在工作,保存事件不會觸發。他們只在根部下面的第一層開火。 我不使用NIB,因爲我以編程方式生成代碼。 – thstart
childVC宣佈在哪裏?在你的AppDelegate的頭文件中?如果是這樣,你是否也可以提供該代碼? – Aaron
我在發佈問題 – thstart