使用导航控制器时关闭视图控制器不起作用

Dismissing View Controller Doesn't Work While Using Navigation Controller

我有两个视图控制器。

第一个嵌入Navigation Controller。 在第一个视图控制器上有 Bar Button Item which is connected by a segue to the second view controller. The segue is set as Push. Once I go to the second view controller there is a Bar Button Item which is an IBAction 并且应该关闭页面,但它没有。

第二视图控制器代码:

import UIKit

class Page2ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func donePressed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

}

在您的 donePressed(_:) 函数中,访问导航控制器并调用 popViewController(_:) 函数。

@IBAction func donePressed(_ sender: Any) {
     navigationController?.popViewController(animated: true)
}