1

MainViewController という名前の UIViewController があります。ViewMaker という名前の別のクラスがあります。ViewMaker クラスには、次のような関数があります。

 -(UIView *)GetContentView
{
UIView *return_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];

UIButton *done_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
done_button.frame = CGRectMake(300, 300, 100, 50);
[done_button setTitle:@"Done" forState:UIControlStateNormal];
[return_view addSubview:done_button];
return return_view;
}

上記の関数では、実際にはテキストビュー、ラベルなどのビューをさらに追加します。

MainViewController クラスでは、上記のメソッドを次のように呼び出しています。

 -(void)CreateViews
 {   
 UIView *content_view = [[[ViewMaker alloc] init] GetContentView];
 [self.view addSubview:content_view];
 }

MainViewController クラスから、完了ボタン (およびテキストビュー、ラベルなどの他のコンポーネント) を含むビューを追加しました。私の問題は、完了ボタンのターゲット関数を追加することです。完了ボタンをクリックすると、ViewMaker クラスから返されたビューのデータに従って、MainViewController でいくつかのアクション (いくつかのビューを追加するなど) を実行したいと考えています。これどうやってするの?前もって感謝します。

4

3 に答える 3

1

あなたのViewMakerクラスはPopupContentMakerと同じだと思いますが、

これを完了ボタンに追加します。

[done_button addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];

PopupContenMaker で:

- (void)doSomething{
if ([self.delegate respondsToSelector:@selector(handleDoneButtonTap)]) {
    [self.delegate performSelector:@selector(handleDoneButtonTap) withObject:self];
}

}

PopupContentMaker でデリゲート プロパティを宣言し、MainViewController をデリゲートにします。

-(void)GetContentView
{   
PopContentMaker *pcm = [[PopupContentMaker alloc] init];

pcm.delegate = self;

[self.view addSubview:[pcm GetContentView]];

}

-(void)handleDoneButtonTap{
//Do action after done button
}
于 2013-11-04T17:58:42.923 に答える
0

ターゲットとアクションをパラメーターとして getContentView メソッドに渡すか、デリゲート パターンを使用できます。

于 2013-11-04T17:58:13.347 に答える