views:

1308

answers:

2

I add a firstView in the AppDelegate :

#import "TestViewAppDelegate.h"
#import "MainViewController.h"
@implementation TestViewAppDelegate

@synthesize window;
@synthesize mainViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [window makeKeyAndVisible];
    [window addSubview:mainViewController.view];
}

- (void)dealloc {
    [mainViewController release];
    [window release];
    [super dealloc];
}

Then I want to switch to the secondView :

#import "MainViewController.h"
#import "SecondViewController.h"

@implementation MainViewController

@synthesize mainViewController, secondViewController;

- (IBAction)viewSwitch
{
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    self.secondViewController = second;
    [second release];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    [mainViewController.view removeFromSuperview];
    [self.view addSubview:secondViewController.view];

    [UIView commitAnimations];
}

- (void)dealloc {
    [mainViewController release];
    [secondViewController release];
    [super dealloc];
}

@end

And then the same thing for switching from secondView to firstView…

The problem is when I switch the view I thought to release is always visible and don't disappear.

Download the full code

+1  A: 
[mainViewController.view removeFromSuperview];
[self.view addSubview:secondViewController.view];

What is "self" here? And why does the MainViewController class have a property named mainViewController? Exploring those questions will likely lead you to the answer here.

Neil Mix
Thanks for your response.I need to add the secondView.xib on the main screen, it's why I ask self.view to add. Is it wrong ?the property mainViewController has been created to remove the firstView.xib that is associated to the MainViewController used. Because I couldn't remove the xib without the property.I didn't find the answer, but still exploring…
MAGE
+1  A: 

A better approach might be to create one top-level parent view controller that you add to the window. It should have a reference to both of your other view controllers and do the swapping instead. If you want your button inside each view to cause the swapping, you could just have your button IBAction for each post a notification that your top level view registers for and responds to.

Matt Long
Thanks for the response.I will try this way too, but need a bit more help. If I add a view controller to the window with IB, do I need to add also a view in the MainWindow.xib that will be the top level view ?
MAGE
Finally I followed this approach.
MAGE
Did you find the answer to your question? This gives a different approach, but doesn't seem to directly answer what wasn't working. I'm trying to figure out a problem similar to yours.
Jacob