ios 在 Swift 中以编程方式返回上一个 ViewController

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

Programmatically go back to previous ViewController in Swift

iosswiftsegue

提问by Christian

I send the user over to a page on a button click. This page is a UITableViewController.

我通过单击按钮将用户发送到页面。这个页面是一个UITableViewController

Now if the user taps on a cell, I would like to push him back to the previous page.

现在,如果用户点击一个单元格,我想将他推回到上一页。

I thought about something like self.performSegue("back")....but this seems to be a bad idea.

我想过类似的事情,self.performSegue("back")....但这似乎是一个坏主意。

What is the correct way to do it?

正确的做法是什么?

回答by Praveen Gowda I V

Swift 3:

斯威夫特 3:

If you want to go back to the previous view controller

如果你想回到之前的视图控制器

_ = navigationController?.popViewController(animated: true)

If you want to go back to the root view controller

如果你想回到根视图控制器

_ = navigationController?.popToRootViewController(animated: true)

回答by Vyacheslav

Swift 3, Swift 4

斯威夫特 3,斯威夫特 4

if movetoroot { 
    navigationController?.popToRootViewController(animated: true)
} else {
    navigationController?.popViewController(animated: true)
}

navigationController is optional because there might not be one.

navigationController 是可选的,因为可能没有。

回答by Fullpower

Swift 3

斯威夫特 3

I might be late in the answer but for swift 3 you can do it this way:

我可能会迟到,但对于 swift 3,你可以这样做:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "< Back", style: .plain, target: self, action: #selector(backAction))

    // Do any additional setup if required.
}

func backAction(){
    //print("Back Button Clicked")
    dismiss(animated: true, completion: nil)
}

回答by Braham Youssef

Swift 4

斯威夫特 4

there's two ways to return/back to the previous ViewController :

有两种方法可以返回/返回到之前的 ViewController :

  1. First case: if you used : self.navigationController?.pushViewController(yourViewController, animated: true) in this case you needto use self.navigationController?.popViewController(animated: true)
  2. Second case: if you used : self.present(yourViewController, animated: true, completion: nil) in this case you needto use self.dismiss(animated: true, completion: nil)
  1. 第一种情况:如果您使用:self.navigationController?.pushViewController(yourViewController, animated: true) 在这种情况下您需要使用self.navigationController?.popViewController(animated: true)
  2. 第二种情况:如果您使用:self.present(yourViewController, animated: true, completion: nil) 在这种情况下您需要使用self.dismiss(animated: true, completion: nil)

In the first case , be sure that you embedded your ViewController to a navigationController in your storyboard

在第一种情况下,请确保您将 ViewController 嵌入到情节提要中的 navigationController

回答by spencer.sm

In the case where you presented a UIViewControllerfrom within a UIViewControlleri.e...

如果您UIViewControllerUIViewControllerie..

// Main View Controller
self.present(otherViewController, animated: true)

Simply call the dismissfunction:

只需调用dismiss函数:

// Other View Controller
self.dismiss(animated: true)

回答by midhun p

swift 5 and above

迅捷 5 及以上

case 1 : using with Navigation controller

案例1:与导航控制器一起使用

self.navigationController?.popViewController(animated: true)

case 2 : using with present view controller

案例 2:与当前视图控制器一起使用

self.dismiss(animated: true, completion: nil)

回答by Pankaj

If Segue is Kind of 'Show' or 'Push' then You can invoke "popViewController(animated: Bool)" on Instance of UINavigationController. Or if segue is kind of "present" then call "dismiss(animated: Bool, completion: (() -> Void)?)" with instance of UIViewController

如果 Segue 是一种“显示”或“推”,那么您可以在 UINavigationController 的实例上调用“popViewController(animated: Bool)”。或者,如果 segue 是一种“存在”,则使用 UIViewController 实例调用“dismiss(animated: Bool, completion: (() -> Void)?)”

回答by Amr Angry

for swift 3 you just need to write the following line of code

对于 swift 3,您只需要编写以下代码行

_ = navigationController?.popViewController(animated: true)

回答by Ahsan

Try this: for the previous view use this:

试试这个:对于以前的视图,使用这个:

navigationController?.popViewController(animated: true)  

pop to root use this code:

弹出根使用此代码:

navigationController?.popToRootViewController(animated: true) 

回答by Gabo MC

Swift 4.0 Xcode 10.0 with a TabViewController as last view

Swift 4.0 Xcode 10.0 以 TabViewController 作为最后一个视图

If your last ViewController is embebed in a TabViewController the below code will send you to the root...

如果您的最后一个 ViewController 嵌入在 TabViewController 中,下面的代码会将您发送到根目录...

navigationController?.popToRootViewController(animated: true)
navigationController?.popViewController(animated: true)

But If you really want to go back to the last view (That could be Tab1, Tab2 or Tab3 view..)you have to write the below code:

但是如果你真的想回到上一个视图(可能是 Tab1、Tab2 或 Tab3 视图..),你必须编写以下代码:

_ = self.navigationController?.popViewController(animated: true)

This works for me, i was using a view after one of my TabView :)

这对我有用,我在我的 TabView 之一之后使用了一个视图:)