开发者

NavigationController: Replacing a pushed UIViewController with another one

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 开发者_如何学Ccan 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜