0

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
Zhen
  • 12,361
  • 38
  • 122
  • 199

3 Answers3

5

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];
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
4

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
    }
}
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
3

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.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • thanks for your response and advise. In fact what I intend to do here is just to hide/unhide the loadingPage view from the display. So for example, if an action is triggered, I want to do something like "loadingPage.view.hidden = NO" to show the loading page, else just keep it hidden. Is this ok? Or do you suggest otherwise? – Zhen Jun 04 '11 at 12:30
  • I'd just get rid of loadingPage and give PhotoTableViewController a loading view that it can manage itself. It seems to be doing almost that already, since it apparently knows when it's loading and when it's not. But if you don't want to do that, give it a pointer to the loadingPage object, and have it tell loadingPage that it has started or stopped loading. That object can then manage the views. Really, do what work for you, but if you're going to the trouble of using a controller object, you should let it control its views. Otherwise, what's it for? – Caleb Jun 04 '11 at 13:40
  • can you advise me on how I can do that? I tried to add create a UIView *subview and added it to the phototableviewcontroller.view, but the subview does not seem to show. Anyway is this the approach you are suggesting? – Zhen Jun 04 '11 at 14:34