从打开视图上的 ModalView 调用方法
基本上我有一个在应用程序启动时加载的 viewController 。在该 VC 中,根据是否有用户数据,我提供一个具有登录名的 ModalView。用户登录后,我可以关闭 modalView,但我想在 opener 上调用一个方法,然后用数据填充表。
我认为从 modalView 我可以做类似的事情,
[self.parentViewController loadInitialData];
[self.dismissModalViewControllerAnimated:YES];
但这不起作用..
有什么建议吗?
Basically I have a viewController that loads at app startup. In that VC, depending on whether there is userdata, I serve up a ModalView with either a login. After the user logs in, I can then dismiss the modalView, but I would like to call a method on the opener that will then populate a table with data.
I thought from the modalView I could do something like
[self.parentViewController loadInitialData];
[self.dismissModalViewControllerAnimated:YES];
but that does not work..
any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是因为 self.parentViewController 的类型为“UIViewController”,并且您的 -loadInitialData 方法在 UIViewController 中不存在。有几种常见的方法可以解决这个问题......从最简单和最不“正确”到最复杂和最“正确”:
1)首先,您需要将 self.parentViewController 转换为父视图控制器的类型。类似于:
2)您可以向模态视图控制器添加一个属性,该属性显式保留对父视图控制器的引用,然后调用 loadInitialData 执行此操作。
然后你可以调用:
3)最复杂但最正确的方法是创建一个委托协议,让你的 ParentViewController 实现该协议,并让你的模态视图控制器保留对委托的引用并以这种方式调用。类似这样:
当您呈现模态视图控制器时,只需设置委托即可。在你的 MyParentViewController 中:
然后在你的模态视图控制器中你可以像这样回调:
The problem is because self.parentViewController is of type "UIViewController" and your -loadInitialData method doesn't exist in UIViewController. There are a couple of common ways to solve this problem... from easiest and least "correct" to most complicated and most "correct":
1) First you need to cast your self.parentViewController to the type of your parent view controller. Something like:
2) You can add a property to your modal view controller that explicitly keeps a reference to your parent view controller and then call loadInitialData doing that.
Then you can call:
3) The most complicated, but most correct way to do it is to create a Delegate protocol, have your ParentViewController implement the protocol and have your modal view controller keep a reference to the delegate and call that way. Something like:
When you present your modal view controller, just set the delegate. In your MyParentViewController:
Then in your modal view controller you can call back like so: