Return 到家 UIViewController

Return to home UIViewController

我可以单击“<返回”按钮 3 次以 return 回到主页 UIViewController。我还在导航中添加了图片 "Home"。当用户单击 "Home" 图像时,我如何 return 到第一个 UIViewController?我想我需要添加一些动作

class DoctorsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let image = UIImage(named: "homeIcon")
        let rightButton = UIBarButtonItem(image: image, style: .done, target: self, action: nil)        
        navigationItem.rightBarButtonItem = rightButton
    }
}

试试这个

override func viewDidLoad() {
    super.viewDidLoad()

    let image = UIImage(named: "homeIcon")
    let rightButton = UIBarButtonItem(image: image, style: .done, target: self, action: #selector(self.backBtnClicked))        
    navigationItem.rightBarButtonItem = rightButton
}

func backBtnClicked(_ sender: UIBarButtonItem) {
       _ = self.navigationController?.popToRootViewController(animated: true) // pop to root controller
 }

用swift3试试

var viewControllers = navigationController?.viewControllers
viewControllers?.removeLast(2)
navigationController?.setViewControllers(viewControllers!, animated: true)