访问当前视图中的子视图
我已通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
UIView
的tag
属性在PhotoTableViewController
中获取loadingPage.view
。在您的
photoViewTable
类中。use
tag
property ofUIView
to get yourloadingPage.view
inPhotoTableViewController
.In your
photoViewTable
class.除非您通过引用或标签对其进行标记,否则您应该能够像这样获取它。
Unless you mark it by reference or a tag, you should be able to get it like this.
您尝试同时使用两个视图控制器:
如果 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:
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.