回到家UIViewController

时间:2017-02-03 10:50:06

标签: ios swift uinavigationbar uinavigationitem

我可以在“< Back”按钮上单击3次以返回到主UIViewController。我还在导航上添加了图像“Home”。当用户点击“Home”图像时,如何返回第一个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
    }
}

2 个答案:

答案 0 :(得分:3)

试试这个

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
 }

答案 1 :(得分:0)

尝试使用swift3

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