PresentModalViewController之后如何访问parentViewController?
我需要在presentMoalViewController 之后访问parentViewController。 这是我在第一个视图控制器中调用以查看第二个视图控制器的方法:
- (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,但它不起作用:
- (void) saveData {
FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"];
[parentView performSelectorInBackground:@selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}
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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在第二个 viewController 中使用第一个 viewController 可以将其自身设置为的委托/协议,那就更好了。您可以在此处将数据传回之前的控制器或简单地进行搜索来找到更多信息。这种设计模式几乎在 iOS 编程中无处不在。
最大的优点是,你的第二个 viewController 变得独立于呈现它的人,并且可以轻松地重用。技术术语是“脱钩”。
It would be much better if you used a delegate/protocol in your second viewController that the first viewController could set itself as. You can find more info here Pass data back to the previous controller or simply doing a search. This design pattern is used virtually everywhere in iOS programming.
The big advantage is that then your second viewController becomes independent of whoever presented it, and can be easily reused. The technical term would be 'decoupling'.
上次我这样做时,我使用了 notification:
在你的父 vc 视图中 didload:
Last time I did this I used notifications:
in your parent vc view didload:
对于
navigationController
,您可以声明一个名为myController
的属性,然后您必须像这样启动您的navigationController
:在您的
parentViewController< /code> 您声明方法的类
在您的
navigationController
中,您可以调用[myController saveData];
这应该有效,如果您有任何疑问,请随时询问!
For
navigationController
you can declare a property called something likemyController
, then you have to initiate yournavigationController
like this:In your
parentViewController
Class you declare your MethodIn your
navigationController
you can then call[myController saveData];
This should work, if you have any questions, feel free to ask!
您可以使用以下方式访问父视图控制器 -
You can access your parent view controller using following-