Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 467
Richard Williams Avatar asked Feb 20 '26 22:02

Richard Williams


1 Answers

You can do with:

[self dismissModalViewControllerAnimated:YES];
like image 138
kappa Avatar answered Feb 22 '26 10:02

kappa