Fourj
Fourj

Reputation: 1897

Can I push a viewcontroller to a UINavigationController in backend

My app's root view controller is a navigation controller, and a ImagePickerController is presented as modal view controller. After the image is picked, I want to push a view controller to the root navigation controller before the ImagePickerController is dismissed. Is this possible? Thanks for any tips :)

===================
I have tried pushing a new viewcontroller before the ImagePickerController is dismissed. But it is not pushed, it seems UINavigationController can be used to push/pop in only if it is the current top view controller. @bala
And if I dismiss ImagePickerController first, then push a view controller, there will be two animations. annoying ~

Upvotes: 0

Views: 937

Answers (3)

Robin
Robin

Reputation: 10011

Well if you are asking for pushing a view controller on top of the current view controller than you can do

[self.navigationController pushViewController:controller animated:YES];

When the image picker is dismissed in this method

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    ...
    [self.navigationController pushViewController:controller animated:YES];
    ....
}

And if you are asking for showing the root view controller than you can use this method

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

But if you are asking to push a view controller to the bottom of the stack then there are no documented methods available as such.

Upvotes: 1

justin
justin

Reputation: 5831

To push a view controller from the root view controller, you just need to set up a delegate. Basically, in your modal view controller's header, insert

id delegate;

and

@property (nonatomic, assign) id delegate;

Synthesize it in the implementation file.

When you create your modal view controller to be presented, set myModalViewController.delegate = self;. Now you can call methods in your root view controller from your delegate. For example, you could have a method in your root view controller called pushVC, so before you dismiss your modal view controller, you can call from within it [self.delegate pushVC];, which will execute whatever you have in that method, such as code to push a view controller from underneath the modal view controller. You may want to set up a @protocol for your delegate method in your modal view controller to alleviate any warnings, but it should work fine without it

Upvotes: 0

bala
bala

Reputation: 415

I would assume that you present the imagepicker controller from a controller that is in the navigation controller stack. So if you have set the imagepicker's delegate to the controller that presented the imagepicker controller, you can do something like this in the imagepicker delegate methods:

[self.navigationController pushViewController:newController animated:NO]; 
//Now dismiss imagepicker controller
[picker.parentViewController dismissModalViewControllerAnimated:YES];

Upvotes: 0

Related Questions