簡體   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