views:

576

answers:

3

Hi,

I am very new to Obj-C and learning iphone development. My question is how to add subview from app delegate. Lets say I added subview called "MainView" from "applicationDidFinishLaunching" method.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

MainViewController *aViewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
self.mainViewController = aViewController;
[aViewController release];

[window addSubview:mainViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];

}

"MainView.xib" file has a button to show its child view. When the button is clicked, it calls "showChildView" method.

- (IBAction)showChildView:(id)sender {
    if (self.childViewController == nil) {
     ChildViewController *childController = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
     self.childViewController = childController;
     [childController release];
    }

    [self.view insertSubview:childViewController.view atIndex:0];
}

From this code, when app launches, it shows "MainView" with a button. But when I clicked the button, the button is still visible as well as the content from the "ChildView.xib" file too.

How can I hide the "MainView" when I pressed the button and show only the contents of the "ChildView"?

Thanks for your help in advance.

A: 

well, you have to remove the original view first, before inserting the new subview, do it this way

- (IBAction)showChildView:(id)sender {
    if (self.childViewController == nil) {
        ChildViewController *childController = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
        self.childViewController             = childController;
        [childController release];
    }
    [self.mainViewControlle.view removeFromSuperView];
    [self.view insertSubview:childViewController.view atIndex:0];
}

Hope this helps.

Zteeth
As this function, "showChildView:" is defined in "MainViewController.m", would "[self.mainViewController.view removeFromSuperView]" message work? When I build the project with this line of code, it has complier error: "request for member 'mainViewController' in something not in a structure or union."Any idea what's happening here?
sungl
how aboutMyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];MainViewController *main = (MainViewController *)[appDelegate mainViewController];UIWindow *window = (UIWindow *)[appDelegate window];[main.view removeFromSuperView];[window addSubview:childViewController.view];
Zteeth
A: 

You might want to check out the Utility App sample -- it demonstrates switching between two views with animation and adding/removing views from parent views.

sehugg
Thank you for the tip! It looks very helpful. :)
sungl
A: 

you might want to create a navigation controller in the main view and than push the childviewcontroller onto it when invoking showChildView. You'll get the back navigation button for free that way

ennuikiller
Thank you. It's another great idea!
sungl