Question

for a better understanding of the following question, here's a little drawing that illustrates the structure of my App: http://grab.by/6jXh

So, basically I have a navigation-based App that uses NavigationController's "pushViewController" method to show Views A and B.

What I'd like to accomplish is to make a transition from view A to view B and vice versa. For example, the user presses the "A" button in the Main View, View A is pushed using the NavigationController. In this view, the user can press a button "Flip to B" and View B replaces View A on the NavigationController's stack (visually, this is done using a flip transition). If the user presses the "back" button on View B, the Main View is shown again. To save used memory, the View(Controller) that is not currently shown has to be dealloced/unloaded/removed.

What would be the appropriate way to do this? Do I need some sort of ContainerViewController or can it be done without?

Thanks.

Was it helpful?

Solution

You can make a ContainerViewController class, then push it like:

ContainerViewController *containerViewController = [[ContainerViewController alloc] initWithFrontView: YES];
[self.navigationController pushViewController: containerViewController animated: YES];
[containerViewController release];

where the class could look something like: (since you can push it with front or back view on top)

- (id)initWithFrontView: (BOOL) frontViewVisible {
    if (self = [super init]) {
        frontViewIsVisible = frontViewVisible;

        viewA = [[UIView alloc] init];
        viewB = [[UIView alloc] init];

        if (frontViewIsVisible) {
            [self.view addSubview: viewA];
        }
        else {
            [self.view addSubview: viewB];
        }


        //add a button that responds to @selector(flipCurrentView:)
    }
    return self;
}

- (void) flipCurrentView: (id) sender {
       [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelegate:self];

        if (frontViewIsVisible == YES) {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView: self.view cache:YES];
            [viewA removeFromSuperview];
            [self.view addSubview: viewB];
        } else {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView: self.view cache:YES];
            [viewB removeFromSuperview];
            [self.view addSubview: viewA];
        }

        [UIView commitAnimations];

        frontViewIsVisible =! frontViewIsVisible;
    }

And don't forget to take care of the memory management. I also recommend you take a look at http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html - that's almost exactly what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top