Richard Williams
Richard Williams

Reputation: 337

Going back to first ViewController from second ViewController

I'm building an app that currently has 3 ViewControllers. One of them is used after a successful login so is not relevant in this question.

I'm using a mixture of Storyboards and building things programmatically when I find Storyboards do not give me the fine control that I need.

The first ViewController is built in my 'MainStoryboard'. It has a login form and an info button at the bottom. I link it up the my AppDelegate by doing the following inside didFinishLaunchingWithOptions:

ViewController *viewController = (ViewController *)self.window.rootViewController;

Because I wanted to force rendering of a UIWebView (another story) I create the second view programmatically. I do the following inside didFinishLaunchingWithOptions:

infoViewController = [[InfoViewController alloc] init];
[infoViewController view];

Inside both of my ViewControllers I setup a link to appDelegate as below:

appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

I have an info button in my first ViewController that takes you to the infoViewController. It calls the following code when tapped:

appDelegate.infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:appDelegate.infoViewController animated:YES];

The above works just fine for me, flips over the screen and shows the InfoViewController.

On my InfoViewController I have a button that should take you back to the login page, I have tried all sorts to get this to work but it just crashes my app. Nothing seems to work. I have tried the following:

appDelegate.viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:appDelegate.viewController animated:YES];

and

[self.navigationController popToRootViewControllerAnimated:YES];

and

[self.navigationController popViewControllerAnimated:YES];

and

[self.navigationController popToViewController:appDelegate.viewController animated:YES];

I suspect the last 3 might be more to do with when you have a navigation view controller and you want to go back to the root? I'm not sure, but either way it does not work. I had this working using storyboards previously so I'm sure it ought to be easy! As mentioned I switched to making the infoViewController programmatically so that I could force the UIWebView to render before the view appeared.

Any help much appreciated.

Upvotes: 2

Views: 5129

Answers (5)

Roman Truba
Roman Truba

Reputation: 4401

Also in presented controller you can use this

if(self.parentViewController)
    [self.parentViewController dismissModalViewControllerAnimated:YES];
else
    [self.presentingViewController dismissModalViewControllerAnimated:YES];

To dissmiss current controller.

Upvotes: 0

Van Du Tran
Van Du Tran

Reputation: 6892

I think the way you're presenting your InfoViewController is wrong. Do it the following way:

In your ViewController, create an action for the info button.:

- (IBAction)infoButtonTapped:(id)sender
{
   InfoViewController *infoViewController = [[InfoViewController alloc] init];
   infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   [self presentModalViewController:infoViewController animated:YES];
}

And in your InfoViewController, in the action of your button that should take you back write this:

- (void)takeBackToViewController
{
   [self dismissModalViewControllerAnimated:YES];
}

Hope it works.

Upvotes: 0

giorashc
giorashc

Reputation: 13713

You should use a main controller for switching between your other view controllers. Change the view of your root controller to one of your other view controllers (apply animations as usual if needed). Hold a pointer to your root controller in your other view controllers and call self.rootController.view = <desired_controller_instance>.view

Upvotes: 1

Dominik Hadl
Dominik Hadl

Reputation: 3619

You should use this.

[self dismissModalViewControllerAnimated:YES];

Upvotes: 5

kappa
kappa

Reputation: 1569

You can do with:

[self dismissModalViewControllerAnimated:YES];

Upvotes: 6

Related Questions