访问当前视图中的子视图

发布于 2024-11-14 08:48:45 字数 589 浏览 1 评论 0原文

我已通过 AppDelegate.m 文件中的以下代码创建了一个带有子视图的视图

//in AppDelegate.m file
//Initializeing the navcon, photoviewtable and default loading page
self.navcon = [[UINavigationController alloc]init];
self.photoViewTable = [[PhotoTableViewController alloc]init];
self.loadingPage = [[LoadingPageViewController alloc]init];

[self.photoViewTable.view addSubview:loadingPage.view];

[navcon pushViewController:photoViewTable animated:NO];
[self.window addSubview:navcon.view];

当我位于 PhotoTableViewController.view 中时,如何访问子视图 LoadingPage.view

I have created a view with a subview via the following code in my AppDelegate.m file

//in AppDelegate.m file
//Initializeing the navcon, photoviewtable and default loading page
self.navcon = [[UINavigationController alloc]init];
self.photoViewTable = [[PhotoTableViewController alloc]init];
self.loadingPage = [[LoadingPageViewController alloc]init];

[self.photoViewTable.view addSubview:loadingPage.view];

[navcon pushViewController:photoViewTable animated:NO];
[self.window addSubview:navcon.view];

How do I access the subview LoadingPage.view when I am in the PhotoTableViewController.view?

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

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

发布评论

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

评论(3

甜是你 2024-11-21 08:48:45

使用 UIViewtag 属性在 PhotoTableViewController 中获取 loadingPage.view

loadingPage.view.tag = 111;
[self.photoViewTable.view addSubview:loadingPage.view];

在您的 photoViewTable 类中。

LoadingPageViewController*  myLoadingPage = (LoadingPageViewController*) [self.view viewWithTag:111];

use tag property of UIView to get your loadingPage.view in PhotoTableViewController .

loadingPage.view.tag = 111;
[self.photoViewTable.view addSubview:loadingPage.view];

In your photoViewTable class.

LoadingPageViewController*  myLoadingPage = (LoadingPageViewController*) [self.view viewWithTag:111];
把人绕傻吧 2024-11-21 08:48:45

除非您通过引用或标签对其进行标记,否则您应该能够像这样获取它。

for ( UIView *subview in self.view.subviews ) {
    if ( [[subview nextResponder] isMemberOfClass:[LoadingPageViewController class]] ) {
        // 'subview' is your view
    }
}

Unless you mark it by reference or a tag, you should be able to get it like this.

for ( UIView *subview in self.view.subviews ) {
    if ( [[subview nextResponder] isMemberOfClass:[LoadingPageViewController class]] ) {
        // 'subview' is your view
    }
}
冷月断魂刀 2024-11-21 08:48:45

您尝试同时使用两个视图控制器:

self.photoViewTable = [[PhotoTableViewController alloc]init];
self.loadingPage = [[LoadingPageViewController alloc]init];

[self.photoViewTable.view addSubview:loadingPage.view];

如果 LoadingPageViewController 是 UIViewController 子类,则上面的代码不正确。除了像 UINavigationViewController 这样的“容器”视图控制器之外,在给定时间应该只有一个视图控制器处于活动状态。请参阅 我的回答 .com/q/5691226/643383">这个SO线程以获得有关情况的更完整的概述。简而言之,UIViewController 旨在管理整个屏幕的内容,并且您从 UIViewController 获得的大部分功能都与管理这些屏幕有关。

不过,为了更直接地回答您的问题,让我们忽略多个 UIViewController 问题,只查看每个对象的职责。由于您已经创建了一个对象(loadingPage)来管理某些视图组,因此 photoViewTable 应该真正让loadingPage 完成其工作,并避免尝试直接操作loadingPage 的视图。为 PhotoTableViewController 提供一个指向 LoadingPageViewController 的指针,以便 photoViewTable 可以向 loadingPage 发送消息。

You're trying to use two view controllers at the same time:

self.photoViewTable = [[PhotoTableViewController alloc]init];
self.loadingPage = [[LoadingPageViewController alloc]init];

[self.photoViewTable.view addSubview:loadingPage.view];

If LoadingPageViewController is a UIViewController subclass, the code above is incorrect. Aside from "container" view controllers like UINavigationViewController, there should only be one view controller active at a given time. See my answer in this SO thread for a more complete rundown on the situation. In short, UIViewController is meant to manage an entire screen's worth of content, and most of the functionality you get from UIViewController relates to managing those screens.

To answer your question more directly, though, lets ignore the multiple UIViewController issue and just look at the responsibility of each object. Since you've created an object (loadingPage) to manage some group of views, photoViewTable should really let loadingPage do its job and avoid trying to manipulate loadingPage's views directly. Give PhotoTableViewController a pointer to a LoadingPageViewController so that photoViewTable can send messages to loadingPage.

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