从第二个 ViewController 回到第一个 ViewController [英] Going back to first ViewController from second ViewController
问题描述
我正在构建一个目前有 3 个 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.
当我发现 Storyboard 不能提供我需要的精细控制时,我混合使用 Storyboard 和以编程方式构建事物.
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.
第一个 ViewController 内置于我的MainStoryboard"中.它有一个登录表单和底部的信息按钮.我通过在 didFinishLaunchingWithOptions 中执行以下操作将其链接到我的 AppDelegate:
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;
因为我想强制渲染 UIWebView(另一个故事),所以我以编程方式创建了第二个视图.我在 didFinishLaunchingWithOptions 中执行以下操作:
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];
在我的两个 ViewControllers 中,我设置了一个指向 appDelegate 的链接,如下所示:
Inside both of my ViewControllers I setup a link to appDelegate as below:
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
我的第一个 ViewController 中有一个信息按钮,可将您带到 infoViewController.点击时调用以下代码:
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];
以上对我来说效果很好,翻转屏幕并显示 InfoViewController.
The above works just fine for me, flips over the screen and shows the InfoViewController.
在我的 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];
和
[self.navigationController popToRootViewControllerAnimated:YES];
和
[self.navigationController popViewControllerAnimated:YES];
和
[self.navigationController popToViewController:appDelegate.viewController animated:YES];
我怀疑当你有一个导航视图控制器并且你想回到根目录时,最后三个可能更重要?我不确定,但无论哪种方式都行不通.我以前使用故事板进行过这项工作,所以我相信它应该很容易!如前所述,我转而以编程方式制作 infoViewController,以便我可以强制 UIWebView 在视图出现之前呈现.
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.
非常感谢任何帮助.
推荐答案
您可以使用:
[self dismissModalViewControllerAnimated:YES];
这篇关于从第二个 ViewController 回到第一个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!