views:

504

answers:

4

Hello,

My iPhone app is tab bar-based, but I would like to fire an action which switches out the tab bar controller view completely and replaces everything in the window with a view from a different XIB file. I would guess this has to be done in the application delegate (since this is the "chief" class), but I don't know the right way to go from there. Does anyone know how to do what I am trying to do?

Thanks in advance!

A: 

Your tab view controller could have it's action method, such as -(IBAction)onChangeView and that method calls a method on the [[UIApplication sharedApplicaton] delegate], such as -(void)toggleScreen.

-(IBAction)onChangeView:(id)sender
{
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate toggleScreen];
}

Then in your app delegate method do something like this:

-(void)toggleScreen
{
    [[[window subviews] objectAtIndex:0] removeFromSuperview];
    [window addSubview:otherView];
}

It may be different if you need transitions, however.

Nick Bedford
A: 

You don't really have to go all the way to the app delegate. You can easily remove your existing views and controllers and add new views. It really does depend on what you are doing. You could, for example, temporarily put aside the existing view structure and go with something totally different, and get back to the original scheme if you want (I'm not sure of the user experience though).

In my apps, I usually have a root view controller or I use my main window to add views to and remove views from. Some views will add other views to themselves or transition another view onto the rootview or main window. Some views close themselves to reveal the view below. The options are limitless, and depend on your needs and architecture.

mahboudz
A: 

Thanks guys! Nick, that's just the code I needed. In case anyone finds this handy, here's the code I used to add animation to switch the view. Just put this in a method in the app delegate:

AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
[[[window subviews] objectAtIndex:0] removeFromSuperview];
[window addSubview:[anotherViewController view]];
[UIView commitAnimations];
Eamon Ford
A: 

I tried this and it worked really well except that I lost my bottom nav bar. Any idea why this is so?

Linda