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;
}

现在在 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];

由于您想要影响的视图控制器是parentViewController,因此您应该能够强制转换和设置abc属性。

于 2011-07-07T06:03:25.883 回答