iPhone - 在父视图控制器中调用方法

发布于 2024-08-12 14:40:13 字数 415 浏览 5 评论 0原文

一直在寻找这个问题的答案,但似乎找不到明确的解决方案 - 希望有人可以在这里提供帮助!

我正在为 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 技术交流群。

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

发布评论

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

评论(2

宛菡 2024-08-19 14:40:13

这是看起来笨重的东西,但它应该展示总体思路:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

其中大部分可以轻松地用 #define 宏替换:

#define CurrentParentViewController [[self.navigationController viewControllers] objectAtIndex:[[self.navigationController viewControllers] count] - 2]

然后您可以按如下方式使用它:

[CurrentParentViewController setBackBarButtonItemTitle:@"New Title"];

如果您这样做,这显然会崩溃并烧毁在导航堆栈中最底部的视图控制器中调用此方法,但想必您会知道何时使用此方法。例如,您可以调用 [[self.navigationController viewControllers] count] 以确保您可以运行它。

Here's something that looks clunky, but it should demonstrate the general idea:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

Much of this can easily be replaced with a #define macro:

#define CurrentParentViewController [[self.navigationController viewControllers] objectAtIndex:[[self.navigationController viewControllers] count] - 2]

You might then use it as follows:

[CurrentParentViewController setBackBarButtonItemTitle:@"New Title"];

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.

£烟消云散 2024-08-19 14:40:13

您可以创建一个单例对象。这篇 cocoawithlove 文章在这个主题上做得非常出色。

You could create a singleton object. This cocoawithlove article does a magnificent job on this topic.

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