以编程方式返回到Swift中的前一个ViewController

时间:2015-02-27 08:29:59

标签: ios swift segue

我通过点击按钮将用户发送到页面。此页面是 UITableViewController

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

我想过像self.performSegue("back")....这样的东西,但这似乎是一个坏主意。

这样做的正确方法是什么?

15 个答案:

答案 0 :(得分:454)

斯威夫特3:

如果您想返回上一个视图控制器

_ = navigationController?.popViewController(animated: true)

如果要返回根视图控制器

_ = navigationController?.popToRootViewController(animated: true)

答案 1 :(得分:42)

Swift 3 Swift 4

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

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

答案 2 :(得分:31)

Swift 3

我可能会在答案中迟到,但对于快速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)
}

答案 3 :(得分:21)

快捷键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

答案 4 :(得分:16)

如果您在UIViewController内提出UIViewController,则......

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

只需拨打dismiss功能:

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

答案 5 :(得分:10)

如果Segue有点&#39; Show&#39;或者&#39; Push&#39;然后你可以调用&#34; popViewController(动画:Bool)&#34;关于UINavigationController的实例。或者,如果segue是&#34;存在&#34;然后调用&#34;解雇(动画:Bool,完成:(() - &gt; Void)?)&#34;使用UIViewController的实例

答案 6 :(得分:7)

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

_ = navigationController?.popViewController(animated: true)

答案 7 :(得分:3)

Swift 5及更高版本

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

self.navigationController?.popViewController(animated: true)

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

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

答案 8 :(得分:2)

尝试一下:  对于以前的视图,请使用以下代码:

navigationController?.popViewController(animated: true)  

pop到root使用此代码:

navigationController?.popToRootViewController(animated: true) 

答案 9 :(得分:1)

使用TabViewController作为最后一个视图的Swift 4.0 Xcode 10.0

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

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

但是,如果您确实要返回到最后一个视图(可以是Tab1,Tab2或Tab3视图。),则必须编写以下代码:

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

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

答案 10 :(得分:0)

有关如何将您的viewController嵌入情节提要中的navigationController的问题:

  1. 打开您的不同viewController所在的情节提要
  2. 点击您要导航控制器开始的viewController
  3. 在Xcode顶部,点击“编辑器”
  4. ->点击嵌入
  5. ->点按“导航控制器

答案 11 :(得分:0)

这对我有用(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")
        }
      }
    }
}

答案 12 :(得分:0)

我是这样做的

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)
}

答案 13 :(得分:0)

我想提出另一种解决此问题的方法。不要使用导航控制器来弹出视图控制器,而要使用展开序列。该解决方案具有一些但非常重要的优点:

  1. 原始控制器可以返回到任何其他目标控制器(而不仅仅是前一个),而无需了解任何有关目标的信息。
  2. 推送和弹出键是在情节提要中定义的,因此视图控制器中没有导航代码。

您可以在Unwind Segues Step-by-Step中找到更多详细信息。在前面的链接中,如何更好地解释了方法,包括如何将数据发送回去,但是在这里,我将做一个简短的解释。

1)转到目的地(而非原点)视图控制器,并添加一个放松的场景:

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

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

Unwind from view controller

3)选择您刚才创建的展开功能:

Select unwind function

4)选择展开顺序并为其命名:

Naming unwind segue

5)转到原始视图控制器的任何位置,然后调用展开命令:

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

当您的导航变得越来越复杂时,我发现这种方法会带来很多好处。

我希望这对某人有帮助。

答案 14 :(得分:-3)

我可以通过在&#34; viewDidDisappear&#34;中编写代码来重定向到根页面。导航控制器,

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