4

parentviewcontroller にデータを送信したいのですが、次のコードがクラッシュします。解決策を教えて

Post *vc;
vc.abc =@"Comment Conttroller";
[self.parentViewController dismissModalViewControllerAnimated:YES];

ここで、Post は、presentViewController:animated:completionメソッドを呼び出しているコントローラー名です。

4

3 に答える 3

21

これを親コントローラーに入れてviewDidLoad

// get register to fetch notification  
[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(yourNotificationHandler:)
                                      name:@"MODELVIEW DISMISS" object:nil];
// --> Now create method in parent class as;
// Now create yourNotificationHandler: like this in parent class
-(void)yourNotificationHandler:(NSNotification *)notice{
    NSString *str = [notice object];
}

あなたの子供のクラスにフォローを入れてください

-(void)dissmissModelView{

    [self dismissModalViewControllerAnimated:YES];
    NSLog(@"DismissModalviewController");

    //raise notification about dismiss 
    [[NSNotificationCenter defaultCenter] 
          postNotificationName:@"MODELVIEW DISMISS" 
                        object:@"Whatever you want to send to parent class"];  
}

モデルビューが閉じられるとすぐにyourNotificationHandlerが実行され、オブジェクトとして渡したものはすべて親クラスでフェッチされます。それでも説明が必要かどうか尋ねてください。

于 2012-02-22T05:27:27.653 に答える
1

これをParentViewControllerの .h ファイルに取り込みます

NSString *strABC;

ParentViewControllerで以下の関数を作成します

-(void)setString:(NSString *)strEntered{
    strABC=strEntered;
}

In Postビューコントローラーは次のようにします:

ParentViewController *objSecond = 
  [[ParentViewController] initwithNibName:@"parentView.xib" bundle:nil];

[objSecond setString:@"Comment Controller"];
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];

ここで、secondViewController viewWillAppearメソッドにこれを記述します。

-(void)viewWillAppear:(BOOL)animated{
      lblUserInput.text = strABC;
}

手書きなので誤字脱字はご容赦ください。この助けを願っています。

使用していない場合はUINavigationContoller、このようなことができます。

SecondViewControler *objSecond = 
  [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
[objSecond setUserInput:txtUserInput.text];
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];
于 2011-07-07T05:56:48.163 に答える
0

vc初期化されていないようです。

あなたはおそらくこれを行うことができます、

vc = (Post *)self.parentViewController;
vc.abc = @"Comment Conttroller";
[vc dismissModalViewControllerAnimated:YES];

影響を与えたいView Controllerは であるため、プロパティparentViewControllerをキャストして設定できるはずです。abc

于 2011-07-07T06:03:25.883 に答える