2013-03-28 42 views
1

我實現一個自定義的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永遠不會觸發

回答

0

我有一個預感,你的觸摸事件不會通過你的子根級別視圖控制器,因爲你沒有在自定義容器中正確使用它們。在didFinishLaunchingWithOptions:完成後,您的ViewController指針也可能不會被保留。沒有提供更多的代碼就無法知道。

[編輯]我根據我通過評論發現的內容澄清了我的答案。

  1. 我/我們不能幫助原始的海報,沒有更多的代碼和更深入的瞭解如何編寫,編譯和運行的應用程序。下面的評論概述了已經提出的一些問題。
  2. 如何爲原來的海報提到,他/她不能在全文,並以某種方式被自動生成它張貼正在生成的AppDelegate代碼目前尚不清楚。
  3. 提供的代碼中沒有提供其指針聲明的變量。它們是:childVC,parentTopVCInnerframeLevel1btnInView。在didFinishLaunchingWithOptions:完成後可能不會保留btnInView,這可能是問題的一部分。
  4. 提供的源代碼似乎不必要地調用didMoveToParentViewController:。這裏沒有UIViewController子類,所以不需要調用它。嘗試刪除它。再次,目前還不清楚如何創建或編寫AppDelegate代碼,或者如果原始的海報可以實際執行此操作。

最後....

如果你想有一個自定義視圖控制器容器類,我會考慮通過蘋果的視圖控制器編程指南閱讀:

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

我肯定會讀標題左側創建自定義容器視圖控制器節它的子部分標題爲實施自定義容器視圖控制器

+0

謝謝。我閱讀了上述鏈接和其他信息。 沒有任何運行時間警告,代碼正在工作,保存事件不會觸發。他們只在根部下面的第一層開火。 我不使用NIB,因爲我以編程方式生成代碼。 – thstart

+0

childVC宣佈在哪裏?在你的AppDelegate的頭文件中?如果是這樣,你是否也可以提供該代碼? – Aaron

+0

我在發佈問題 – thstart