russell
russell

Reputation: 111

iOS4 how to restart view controller

I wish to restart a view controller.

What I've done is to pop the current view controller off the navigation stack, and push a new instance of the view controller onto the stack. However, this does not work. The current view controller is popped off the navigation stack but, the new instance is not pushed onto the stack.

Here is my code snippet:

[[self navigationController] popViewControllerAnimated:YES];

VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
[videoPlayer setMedia:media];

[[self navigationController] pushViewController:videoPlayer animated:YES];
[videoPlayer release];
videoPlayer = nil;

NSLog(@"Restarting view controller...");

Any idea what could be wrong?

Upvotes: 2

Views: 787

Answers (4)

cduhn
cduhn

Reputation: 17918

Based on your code, it looks like you're trying to pop the current view controller and then immediately re-push it, with animations in both directions. I can't imagine why you'd want to do this, but setting that aside for the moment, here's how you can make it work.

First, add <UINavigationControllerDelegate> to your @interface declaration. Then:

- (void)repushViewController {
    self.navigationController.delegate = self;
    [[self navigationController] popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    navController.delegate = nil;
    VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
    [videoPlayer setMedia:media];

    [navController pushViewController:videoPlayer animated:YES];

    [videoPlayer release];
}

Upvotes: 3

petert
petert

Reputation: 6692

Is, or was, the view controller being "popped" at the top of the stack? If so I might suggest a change of design - I'd use this method of UINavigationController:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

to swap you're view controllers around, and with animation if you still require.

VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];

[videoPlayer setMedia:media];

[[self navigationController] setViewControllers:[NSArray arrayWithObject:videoPlayer]
                                      animation:YES];

[videoPlayer release];

Upvotes: 0

karim
karim

Reputation: 15589

If you are asking about the VideoPlayerViewController,

[[self navigationController] popViewControllerAnimated:YES];

goes to the previous view controller. So code in the current view controller may not be executed. The new instance of VideoPlayerViewController should be instantiated in the previous view controller.

Upvotes: 0

Ilanchezhian
Ilanchezhian

Reputation: 17478

I guess u need to give some time interval between the calls. Since u are doing this with animation, it would require some time to finish the first call itself.

So first try, [[self navigationController] popViewControllerAnimated:NO];.

Upvotes: 0

Related Questions