查看 iPhone 中的层次结构问题

发布于 2024-12-03 19:23:17 字数 302 浏览 3 评论 0原文

无论如何,我不会说这是一个好的设计。我有点继承现有的东西。无论如何,有一个TabController。在这一选项卡中,有两个基于 UISegmentController 按需加载的视图。这两个 ViewController 都是另一个具有我需要的方法的 ViewController 的子类。

当我在 TabController 中时,我想创建一个使用两个 ViewController 的一些超类方法的方法。由于选项卡是按需加载的,因此如何访问该选项卡的当前 ViewController?我是否需要获得基本控制器类型的引用,并在按需加载时将其设置为当前视图控制器?谢谢。

I'm not going to say this is good design by any means. I'm kind of inheriting something that is existing. Anyway, there is a TabController. One this one tab, there are two views that get loaded on demand based on a UISegmentController. Both of these two ViewControllers are subclasses of another ViewController that has methods I need.

When I'm in the TabController, I want to create a method that uses some of the superclass methods of the two ViewControllers. How do I get access to the tab's current ViewController since it's loaded on demand? Do I need to have a reference of the base controller type, and just have it set to the current view controller when it gets loaded on demand? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

剧终人散尽 2024-12-10 19:23:17

要让活动选项卡显示在屏幕上,您可以使用 [self.tabBarController selectedViewController] 这将为您提供 UIViewController 参考。如果您想使用这些方法,那么您可以将其强制转换为 ViewController 超类,然后像这样调用它的方法(其中 self.tabBarController 是您的选项卡栏控制器):

MySuperClassViewController *viewC = (MySuperClassViewController *)[self.tabBarController selectedViewController];
[viewC someMethodDeclaredHere];

To get the active tab showing on the screen, you could use [self.tabBarController selectedViewController] which will give you a UIViewController Reference. If you want to use the methods then you can cast it to your ViewController Superclass and then call methods on it like so (where self.tabBarController is your Tab Bar Controller):

MySuperClassViewController *viewC = (MySuperClassViewController *)[self.tabBarController selectedViewController];
[viewC someMethodDeclaredHere];
新雨望断虹 2024-12-10 19:23:17

首先,使用一些术语来消除一些潜在的混淆:我假设您的意思是控制各种视图控制器的 UITabBarController 。另外,您似乎使用的是 UISegmentedControl,它不是视图控制器,而是 UIView 的子类。我希望这些只是拼写错误而不是概念问题。

关于“当我在选项卡控制器中时”的含义仍然存在一些含糊之处。我假设您想将代码放入表示 UITabBarController 的类中。但为什么?只需将这些方法放入适当的视图控制器中,或者如果之前必须完成这些方法,则放入您的应用程序委托中。但是,如果这些方法位于视图控制器超类中,则它们应该与该视图控制器所需的逻辑有关。

如果您在其他地方(即视图控制器之外)需要这些方法,请考虑创建一个单独的 #include 文件并将方法放在那里。或者,您可以使用应用程序委托,该委托可通过 [[UIApplication sharedApplication] delegate] 方便地访问。

First, a little bit of terminology to clear up some potential confusion: I assume you mean a UITabBarController which controls various view controllers. Also, it seems you are using a UISegmentedControl which is not a view controller but a subclass of UIView. I hope these are just typos and not conceptional problems.

There is still some ambiguity about what you mean with "when I am in the tab controller". I assume you want to put code into the class representing the UITabBarController. But why? Just put these methods into the appropriate view controller, or if it is something that has to be done before, into your app delegate. However, if these methods are in a view controller superclass, they should have something to do with the logic necessary for this view controller.

If you need the methods elsewhere, i.e. outside your view controllers, consider creating a separate #include file and putting the methods there. Alternatively, you can use your app delegate which is conveniently accessible through [[UIApplication sharedApplication] delegate].

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