iOS:返回上一个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30257091/
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-08-31 05:59:03  来源:igfitidea点击:

iOS: Go back to previous view

iosswiftviewcontroller

提问by Jasper

I have two views like this in Story Board:

我在 Story Board 中有两个这样的视图:

[View1] -> [Navigation Controller] -> [View2]

[视图1] -> [导航控制器] -> [视图2]

To go from View1(has tableview) to View 2, i do this on click of a row on View1:

要从 View1(有 tableview)转到 View 2,我在点击 View1 上的一行时执行此操作:

self.performSegueWithIdentifier("showmyview2", sender:self)

This works.

这有效。

Now i have a button on View 2, which when clicked should take me back to previous view i.e View 1.

现在我在视图 2 上有一个按钮,点击后应该会带我回到上一个视图,即视图 1。

I tried this:

我试过这个:

 navigationController!.popViewControllerAnimated(true)

But this does not work. How do i go back to first view?

但这不起作用。我如何返回到第一个视图?

回答by Mukesh

First Add a navigation controller to your First view1 through storyboard.(Editor->Embed Navigation controller), If navigation is not present.

首先通过故事板将导航控制器添加到您的第一个视图。(编辑器->嵌入导航控制器),如果导航不存在。

OR

或者

self.dismissViewControllerAnimated(true, completion: nil);

Call this function and try instead of navigation popviewcontroller

调用此函数并尝试代替导航 popviewcontroller

回答by Dharmesh Kheni

Use this code to present first view in your button action:

使用此代码在按钮操作中显示第一个视图:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("FirstView") as! TableViewController
self.presentViewController(vc, animated: true, completion: nil)

And don't forget to give Id to your First view.

并且不要忘记为您的第一个视图提供 Id。

Select your firstView into storyBoard and then click on Identity Inspector and you can assign StoryBoard ID like show into below Image.

选择您的 firstView 进入 storyBoard,然后单击 Identity Inspector,您可以分配 StoryBoard ID,如下图所示。

enter image description here

在此处输入图片说明

回答by Saneesh

self.dismiss(animated: true, completion: nil); this will work in swift 3

self.dismiss(动画:true,完成:nil); 这将在 swift 3 中工作