1

カスタム UIViewController Containment を実装しています。

また、階層の下位にある子ビュー コントローラーのビューにボタンを追加しています。

問題は、ボタンが下位階層の子ビュー コントローラーにある場合、ボタン イベントが発生しないことです。これらは、ルート レベルの 1 つ下のレベル (parentTopVc) まで配置された場合にのみ機能します。

アーキテクチャは次のようになります。

ルートは次のとおりです: self.window.rootViewController = rootVc

階層:

0-----rootVc

1 -----------parentTopVc <--- ボタン イベントが発生する

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 は起動しません

4

1 に答える 1

0

カスタムコンテナでタッチイベントを正しく使用していないため、タッチイベントがサブルートレベルのビューコントローラーに到達していないという予感があります。完了後に ViewController ポインターが保持されない可能性もありますdidFinishLaunchingWithOptions:。より多くのコードを提供しない限り、知る方法はありません。

[編集]コメントで見つけたものに基づいて、回答を明確にしています。

  1. 私/私たちは、コードを追加し、アプリがどのように作成、コンパイル、実行されているかをより深く理解することなく、元の投稿者を助けることはできません. 以下のコメントは、既に尋ねられた質問の一部を概説しています。
  2. AppDelegate コードがどのように生成されているかは明確ではありません。元の投稿者は、コード全体を投稿することはできず、何らかの方法で自動的に生成されていると述べています。
  3. 提供されたコードには、ポインター宣言が提供されていない変数があります。それらは: childVCparentTopVCInnerframeLevel1、およびbtnInViewです。問題の一部である可能性がありますbtnInViewdidFinishLaunchingWithOptions:
  4. 提供されたソース コードは、不必要に を呼び出しているようdidMoveToParentViewController:です。ここには UIViewController サブクラスがないため、呼び出す必要はありません。削除してみてください。繰り返しますが、AppDelegate コードがどのように作成または記述されているか、または元の投稿者が実際にそれを実行できるかどうかは明確ではありません。

ついに ....

カスタム ビュー コントローラー コンテナー クラスが必要な場合は、Apple のビュー コントローラー プログラミング ガイドを読むことを検討します。

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

左側の「カスタム コンテナー ビュー コントローラーの作成」というタイトルのセクションと、「カスタム コンテナー ビュー コントローラーの実装」というタイトルのサブセクションを必ずお読みください。

于 2013-03-28T23:01:37.833 に答える