presentModalViewController之后如何访问parentViewController? [英] How to access to parentViewController after presentModalViewController?

查看:68
本文介绍了presentModalViewController之后如何访问parentViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在presentMoalViewController之后访问parentViewController. 这是我在firstViewController中调用以查看secondViewController的方法:

I need to access to parentViewController after presentMoalViewController. This is my method that I call in the firstViewController to view the secondViewController:

- (void)viewData {

    SecondViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"select_data"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
    navigationController.navigationBar.barStyle = UIBarStyleBlack;

    UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(saveData)] autorelease];
    viewCtrl.navigationItem.rightBarButtonItem = salvaButton;

    UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:@selector(backView)] autorelease];
    viewCtrl.navigationItem.leftBarButtonItem = annullaButton;

    [self presentModalViewController:navigationController animated:YES];

}

当我单击saveButton时,我尝试以这种方式访问​​parentViewController,但它不起作用:

When I click on saveButton, I try to access to parentViewController in this way, but it not work:

- (void) saveData {

    FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
    parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"];
    [parentView performSelectorInBackground:@selector(myMethod) withObject:nil];

    [self dismissModalViewControllerAnimated:YES];

}

推荐答案

上次我使用通知时是这样的:

Last time I did this I used notifications:

[[NSNotificationCenter defaultCenter] postNotificationName:@"saveData" object:dataString];

在您的父vc视图中didload:

in your parent vc view didload:

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

这篇关于presentModalViewController之后如何访问parentViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆