xcode 从第二个 ViewController 回到第一个 ViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9721771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:39:50  来源:igfitidea点击:

Going back to first ViewController from second ViewController

objective-ciosxcodeuiviewcontroller

提问by Richard Williams

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.

我正在构建一个当前有 3 个 ViewController 的应用程序。其中之一在成功登录后使用,因此与此问题无关。

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.

当我发现 Storyboards 没有给我我需要的精细控制时,我混合使用 Storyboards 和以编程方式构建东西。

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 内置于我的“MainStoryboard”中。它在底部有一个登录表单和一个信息按钮。我通过在didFinishLaunchingWithOptions 中执行以下操作将其链接到我的 AppDelegate :

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:

因为我想强制渲染 UIWebView(另一个故事),所以我以编程方式创建了第二个视图。我在didFinishLaunchingWithOptions 中执行以下操作:

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

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

在我的两个 ViewControllers 中,我设置了一个指向 appDelegate 的链接,如下所示:

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:

我的第一个 ViewController 中有一个信息按钮,可以将您带到 infoViewController。点击时它会调用以下代码:

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.

以上对我来说很好用,翻转屏幕并显示 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:

在我的 InfoViewController 上,我有一个按钮可以将您带回登录页面,我尝试了各种方法来使其正常工作,但它只会使我的应用程序崩溃。似乎没有任何效果。我尝试了以下方法:

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.

我怀疑当你有一个导航视图控制器并且你想回到根目录时,最后三个可能更多?我不确定,但无论哪种方式都行不通。我以前使用故事板进行过这项工作,所以我相信它应该很容易!如前所述,我转而以编程方式制作 infoViewController,以便我可以在视图出现之前强制 UIWebView 呈现。

Any help much appreciated.

非常感谢任何帮助。

回答by kappa

You can do with:

你可以这样做:

[self dismissModalViewControllerAnimated:YES];

回答by Dominik Hadl

You should use this.

你应该使用这个。

[self dismissModalViewControllerAnimated:YES];

回答by giorashc

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

您应该使用主控制器在其他视图控制器之间切换。将根控制器的视图更改为其他视图控制器之一(如果需要,照常应用动画)。在其他视图控制器中保持指向根控制器的指针并调用self.rootController.view = <desired_controller_instance>.view

回答by Van Du Tran

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

我认为您呈现 InfoViewController 的方式是错误的。请按以下方式进行:

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

在您的 ViewController 中,为信息按钮创建一个操作。:

- (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:

在您的 InfoViewController 中,在应该带您返回的按钮操作中写下:

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

Hope it works.

希望它有效。

回答by Roman Truba

Also in presented controller you can use this

同样在呈现的控制器中,您可以使用它

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

To dissmiss current controller.

解除电流控制器。