迭代“主动”视图控制器

发布于 2024-08-28 13:50:08 字数 414 浏览 9 评论 0原文

Ola Folks,

在 iPhone 应用程序中,我使用 addSubView:SomeViewController.view 方法显示不同的视图。

我希望至少能够记录正在显示的视图层次结构中的视图控制器。我希望能够获得特定视图控制器的句柄。

我知道如何迭代视图,只是不知道如何访问这些视图的视图控制器。我正在寻找能够为我提供 UINavigationController::ViewControllers 所具有的视图控制器访问类型的东西。

我以为我可以逃脱:

for (UIViewController* oVC in [self.view subviews])

但这并没有达到预期的效果。

如果有人有办法做到这一点,请与我分享。

-isdi-

Ola Folks,

In an iPhone application I am displaying different views by using the addSubView:SomeViewController.view method.

I want to be able to, at the very least, log the view controllers that are in the view hierarchy that is being displayed. I would prefer to be able to get a handle to a specific view controller.

I know how to iterate the views, I just do not see how to access the view controllers of those views. I am looking for something that will give me the type of access to the view controllers that UINavigationController::ViewControllers does.

I thought I could get away with:

for (UIViewController* oVC in [self.view subviews])

but this is not having the intended effect.

If someone has a way of doing this, please share it with me.

-isdi-

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

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

发布评论

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

评论(2

浅唱々樱花落 2024-09-04 13:50:09

视图不保留对其视图控制器的引用(或者通常了解有关视图控制器的任何信息),因此您必须自己跟踪该映射。如果将所有视图控制器保存在数组 viewControllers 中,您可以执行以下操作:

- (UIViewController *) viewControllerForView:(UIView *)view {
    for (UIViewController *viewController in viewControllers)
        if (viewController.view == view)
            return viewController;
    return nil;
}

A view doesn't keep a reference to its view controller (or know anything about view controllers in general), so you'll have to keep track of that mapping yourself. If you keep all of your view controllers in an array viewControllers, you could do something like:

- (UIViewController *) viewControllerForView:(UIView *)view {
    for (UIViewController *viewController in viewControllers)
        if (viewController.view == view)
            return viewController;
    return nil;
}
烧了回忆取暖 2024-09-04 13:50:09

视图与拥有它的视图控制器交互的标准方法是将视图控制器设置为视图的委托或操作目标。该视图被设计为不包含有关委托或操作目标的任何详细信息。

如果您已经实现了自己的视图,只需添加一个成员来保存对视图控制器的引用。或者为视图采用委托模型,这样委托是什么类就无关紧要了。

如果您将视图视为堆栈并希望在其旁边维护一组视图控制器(与 UINavigationController 为您所做的类似),则必须手动执行此操作。将 addSubview:viewController.view 的每次调用与对 [myViewControllerArray addObject:viewController] 的调用结合起来,并在从视图层次结构中删除视图时从数组中删除 viewController。

The standard way for a view to interact with the view controller that owns it is by having the view controller set as the delegate or action target of the view. The view is designed not to have any details about the delegate or action target.

If you have implemented your own view, just add a member to hold a reference to the view controller. Or adopt a delegate model for the view so that it does not matter what class the delegate is.

If you are treating the views as a stack and want to maintain a stack of view controllers along side it, similar to what UINavigationController does for you, you must do so manually. Couple every call of addSubview:viewController.view with a call to [myViewControllerArray addObject:viewController] and remove the viewController form the array when the view is removed from the view hierarchy.

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