0

私は次の状況にあります: ここに画像の説明を入力してください

はのSecondViewController中にありFirstViewControllerます。ここで、別のサブクラスをに追加しますが、画像のようにクラスFirstViewControllerから追加します。secondViewControllerここに画像の説明を入力してください

私は探していましたが、これは不可能だと思います。FirstViewControllerをインスタンス化し、「ビューからサブビュー」にアクセスしてサブビューとして追加しようとしましたが、機能しませんでした。

FirstViewController *viewController = [[FirstViewController alloc] init];
[self.view addSubview:[viewController viewToAddAsSubView]];

ヒント/解決策はありますか?

ありがとう!

4

2 に答える 2

3

これは、NSNotificationCenterを使用して実際に可能です。以下の例:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(METHOD:) name:@"LISTEN_TO_VIEW_2" object:nil];

上記のコードをビュー1に配置して、ビュー2を「リッスン」し、ビュー1のメソッドを実行して、ビュー1で必要なものを追加/編集する必要があるという通知を送信する必要があります。

[[NSNotificationCenter defaultCenter] postNotificationName:@"LISTEN_TO_VIEW_2" object:nil];

上記のコードは、ビュー1に通知を送信します。次に、ビュー1には、次のようなメソッドがあります。

-(void)METHOD:(id)sender {
//do something here
}
于 2012-08-01T02:37:10.703 に答える
0

考えられる1つの方法は、SecondViewControllerのビューのsuperViewを使用することです。これは非常に簡単な方法です。

[[self.view superview] insertSubview:theView aboveSubview:self.view];

別の方法は、デリゲートを使用することです。SecondViewControllerでデリゲートを次のように宣言できます

@protocol SecondViewControllerDelegate : NSObject
{
    - (void)requestInsertView:(UIView*)view aboveView:(UIView*)baseView;
}

@interface SecondViewController <...>

@property (nonatomic, assign) id<SecondViewControllerDelegate>superViewDelegate;
@end;

そして、FirstViewControllerの宣言を変更して、SecondViewControllerDelegateを実装します。

@interface FirstViewController <SecondViewControllerDelegate, ...> 

@implement FirstViewController

- (void)requestInsertView:(UIView*)view aboveView:(UIView*)baseView
{
    [self.view insertView:view aboveSubview:baseView];
}
@end;

SecondViewControllerが作成されたら、そのsuperViewDelegateをFirstViewControllerのインスタンスに設定します。

SecondViewControllerからビューを追加する必要がある場所で、

[self.superViewDelegate requestInsertView:view aboveView:self.view];
于 2012-08-01T03:42:38.563 に答える