iPhone - 在父视图控制器中调用方法
一直在寻找这个问题的答案,但似乎找不到明确的解决方案 - 希望有人可以在这里提供帮助!
我正在为 iPhone 构建一个多视图应用程序。我创建了一个名为“MainViewController”的 UIViewController,它根据需要加载到其他视图中。屏幕之间有相当多的导航,所以我想做的是在 MainViewController 中有一个“ViewSwitcher”方法,子视图控制器可以调用该方法来要求视图切换到另一个子视图。
如何从子控制器调用“ViewSwitcher”方法?我目前已经设法通过在每个子视图控制器中创建一个 UIView *“parent”属性并在首次加载子视图时填充它来实现此目的。然后我可以使用 [parent ViewSwitcher] 调用该方法。这看起来有点笨重,我担心我正在创建一些可怕的递归结构。有更好的办法吗?
提前致谢。
have hunted around for an answer to this one, but can't seem to find a definitive solution - hoping someone can help here!
I'm building a multiview app for the iPhone. I've created a UIViewController called "MainViewController", which loads in other views as required. There's quite a lot of navigation between screens, so what I'd like to do is have a "ViewSwitcher" method in MainViewController that the child view controllers can call to ask for the view to switch away to another child view.
How can I call the "ViewSwitcher" method from the child controllers? I've managed to achieve this at present by creating an UIView *"parent" property in each child view controller, and populating this on first loading the child view. I can then call the method using [parent ViewSwitcher]. This seems a bit clunky, and I'm concerned I'm creating some horribly recursive structure. Is there a better way?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是看起来笨重的东西,但它应该展示总体思路:
其中大部分可以轻松地用
#define
宏替换:然后您可以按如下方式使用它:
如果您这样做,这显然会崩溃并烧毁在导航堆栈中最底部的视图控制器中调用此方法,但想必您会知道何时使用此方法。例如,您可以调用
[[self.navigationController viewControllers] count]
以确保您可以运行它。Here's something that looks clunky, but it should demonstrate the general idea:
Much of this can easily be replaced with a
#define
macro:You might then use it as follows:
This would obviously crash and burn if you call this at the bottom-most view controller in the navigation stack, but presumably you would know when you're using this approach. You could, for example, call
[[self.navigationController viewControllers] count]
to make sure you can run this.您可以创建一个单例对象。这篇 cocoawithlove 文章在这个主题上做得非常出色。
You could create a singleton object. This cocoawithlove article does a magnificent job on this topic.