2013-03-28 3 views
1

사용자 정의 UIViewController 포함을 구현 중입니다.iOS : 사용자 정의 UIViewController 억제 단추 이벤트가 작동하지 않습니다.

또한 자식보기 컨트롤러의보기 아래쪽에 단추를 추가하고 있습니다.

버튼 이 계층 구조에서 하위보기 컨트롤러에있을 때 단추 이벤트가 실행되지 않는 것이 문제입니다. 루트 수준 (parentTopVc)보다 한 수준 아래에 배치 된 경우에만 을 처리합니다.

아키텍처는 다음과 같이이다 : self.window.rootViewController = rootVc

계층 :

뿌리에있다

0 ----- rootVc

1 --- -------- parentTopVc < --- 버튼 이벤트가 발생합니다.

2 ---------------------- firstTopVc < --- 버튼 이벤트가 아닙니다. 화재

2 ---------------------- secondTopVc < --- 버튼 이벤트는

- (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 <=="); 
} 

AppDelegate.m을 발사하지 마십시오

doShowNextTopVc never fire

답변

0

사용자 정의 컨테이너에서 올바르게 사용하지 않아 터치 이벤트가 서브 루트 레벨보기 컨트롤러로 전달되지 않는 경우가 있습니다. didFinishLaunchingWithOptions:이 완료된 후 ViewController 포인터가 유지되지 않을 수도 있습니다. 더 많은 코드를 제공하지 않으면 알 수있는 방법이 없습니다.

[편집] 나는 코멘트를 통해 발견 한 것을 바탕으로 내 대답을 분명히하고있다.

  1. 필자는 더 많은 코드없이 응용 프로그램을 작성하고 컴파일하고 실행하는 방법을 더 깊이 이해하지 않고서는 원래의 포스터를 도울 수 없습니다. 아래의 설명은 이미 제기 된 질문 중 일부를 요약 한 것입니다.
  2. AppDelegate 코드가 원래 포스터로 게시되어 전체적으로 게시 할 수 없으며 자동으로 생성된다는 점에서 명확하지 않습니다.
  3. 제공된 코드에 제공된 포인터 선언이 제공되지 않는 변수가 있습니다. 그들은 childVC, parentTopVCInnerframeLevel1btnInView입니다. btnInViewdidFinishLaunchingWithOptions: 완료 후에 유지되지 않을 수 있으며 이는 문제의 일부일 수 있습니다.
  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

관련 문제