2

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?

petert
  • 6,672
  • 3
  • 38
  • 46
russell
  • 111
  • 1
  • 7

4 Answers4

3

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];
}
cduhn
  • 17,818
  • 4
  • 49
  • 65
  • @russell, if you still want your described behaviour?, this is a working approach; added the `release`. – petert Jul 15 '11 at 10:53
  • 1
    Actually I had removed his release and used an autorelease instead (out of personal preference), so this edit creates an overrelease bug. I'll remove my autorelease so that the code is more similar to his original example. – cduhn Jul 15 '11 at 11:52
  • Okay, didn't spot that, by scrolling right in the code snippet. I didn't want any likely Objective-C beginners to miss a release by just cutting-and-pasting you're code, then complaining later it leaked memory! – petert Jul 15 '11 at 12:33
0

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];.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
0

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.

karim
  • 15,408
  • 7
  • 58
  • 96
0

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];
petert
  • 6,672
  • 3
  • 38
  • 46