繁体   English   中英

以编程方式 go 回到 Swift 中的上一个 ViewController

[英]Programmatically go back to previous ViewController in Swift

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

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

我想到了self.performSegue("back")....但这似乎是个坏主意。

正确的做法是什么?

斯威夫特 3:

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

_ = navigationController?.popViewController(animated: true)

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

_ = navigationController?.popToRootViewController(animated: true)

如果您不使用导航控制器,请使用以下代码。

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

您可以根据需要设置动画值。

斯威夫特 3 ,斯威夫特 4

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

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

斯威夫特 3

我可能迟到了答案,但对于 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)
}

斯威夫特 4

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

  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)

在第一种情况下,确保将 ViewController 嵌入到故事板中的 navigationController

迅捷5及以上

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

self.navigationController?.popViewController(animated: true)

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

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

在您从UIViewController中呈现UIViewController的情况下,即..

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

只需调用dismiss函数:

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

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

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

_ = navigationController?.popViewController(animated: true)

这个对我有用(Swift UI)

struct DetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

  var body: some View {
      VStack {
        Text("This is the detail view")
        Button(action: {
          self.presentationMode.wrappedValue.dismiss()
        }) {
          Text("Back")
        }
      }
    }
}

试试这个:对于上一个视图,使用这个:

navigationController?.popViewController(animated: true)  

弹出到根使用此代码:

navigationController?.popToRootViewController(animated: true) 

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

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

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

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

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

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

有关如何将 viewController 嵌入故事板中的 navigationController 的问题:

  1. 打开不同viewController所在的故事板
  2. 点击您希望导航控制器开始的 viewController
  3. 在 Xcode 顶部,点击“编辑器”
  4. -> 点击嵌入
  5. -> 点击“导航控制器

我想提出另一种解决这个问题的方法。 不要使用导航控制器来弹出视图控制器,而是使用 unwind segues。 此解决方案有一些但非常重要的优点:

  1. 源控制器可以返回到任何其他目标控制器(不仅仅是前一个),而无需了解任何关于目标的信息。
  2. Push 和 pop segues 是在 storyboard 中定义的,所以在你的视图控制器中没有导航代码。

您可以在Unwind Segues Step-by-Step中找到更多详细信息。 how to在前面的链接中解释得比较好,包括如何发回数据,但这里我会做一个简单的解释。

1)转到目标(不是源)视图控制器并添加一个展开segue:

    @IBAction func unwindToContact(_ unwindSegue: UIStoryboardSegue) {
        //let sourceViewController = unwindSegue.source
        // Use data from the view controller which initiated the unwind segue
    }

2)CTRL从视图控制器本身拖动到原始视图控制器中的退出图标:

从视图控制器中放松

3) 选择刚才创建的 unwind 函数:

选择展开功能

4) 选择 unwind segue 并为其命名:

命名 unwind segue

5) 转到原始视图控制器的任意位置并调用 unwind segue:

performSegue(withIdentifier: "unwindToContact", sender: self)

当您的导航开始变得复杂时,我发现这种方法会带来很多回报。

我希望这可以帮助别人。

我是这样做的

func showAlert() {
    let alert = UIAlertController(title: "Thanks!", message: "We'll get back to you as soon as posible.", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
        self.dismissView()
    }))

    self.present(alert, animated: true)
}

func dismissView() {
    navigationController?.popViewController(animated: true)
    dismiss(animated: true, completion: nil)
}

如果要关闭前两个视图控制器,只需像这样调用 popViewController 两次:

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

是的,我使用的是我刚刚从我的栏按钮项目添加了一个标准的 segue 回到我想要 go 的视图 controller 。 然后我把它联系起来并调用 performSegue(identifier: K.MyIdentifire, sender: self)。 我还使用了一个常量文件(K),以便我的 segue 标识符是硬编码的,所以我不能打错字

我可以通过在导航控制器的“viewDidDisappear”中编写代码来重定向到根页面,

override func viewDidDisappear(_ animated: Bool) { self.navigationController?.popToRootViewController(animated: true) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM