关闭控制器时将值传递给父控制器

发布于 2024-11-18 23:53:31 字数 274 浏览 4 评论 0原文

我想将数据发送到parentviewcontroller,但以下代码崩溃了。给我解决方案

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

在这里,Post 是我调用 presentViewController:animated:completion 方法的控制器名称。

I want to send data to parentviewcontroller but the following code crashes. Give me the solution

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

Here, Post is the controller name from where I am calling the presentViewController:animated:completion method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱要勇敢去追 2024-11-25 23:53:32

将其放入您的父控制器中的 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 就会被执行,并且您作为对象传递的任何内容都将被获取你的父类。请询问是否还需要一些澄清。

Put this in your parent controller in 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];
}

Put following to your child class where

-(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"];  
}

as soon as model view get dismiss yourNotificationHandler get executed and whatever you pass as an objet will get fetch in your parent class. Please ask if still need some clarification.

断爱 2024-11-25 23:53:32

将其放入 ParentViewController 中的 .h 文件中,

NSString *strABC;

ParentViewController Now In Post 视图控制器中创建以下函数

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

,如下所示:

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

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

Now, In 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];

Take this in .h file in ParentViewController

NSString *strABC;

Make below function in ParentViewController

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

Now In Post view controller do like this:

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

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

Now, In secondViewController viewWillAppear method write this.

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

Please check spelling mistakes as I've hand written this. Hope this help.

If you are not using UINavigationContoller then you can do something like this.

SecondViewControler *objSecond = 
  [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
[objSecond setUserInput:txtUserInput.text];
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];
拥抱没勇气 2024-11-25 23:53:32

vc 似乎没有初始化。

您也许可以这样做,

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

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

vc doesn't seem to be initialized.

You can probably do this,

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

Since the view controller you want to affect is the parentViewController, you should be able to cast and set the abc property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文