简体   繁体   中英

UIBARBUTTON back action

let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("HomeTabController")))

self.navigationItem.leftBarButtonItem = backButton

The above code is the creation of button in navigationController but I can create a button cannot write a specific view controller to open.

I have tried with popViewController and popToRootViewController action, need a specific code for opening a particular viewController in swift, with the help of the particular viewController storyboard id and viewcontrollername.

You need to add func name in #selctor() (Swift 4 version)

let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goToViewController(_:)))

self.navigationItem.leftBarButtonItem = backButton

You need to create a func.

@objc func goToViewController(_ sender: UIBarButtonItem) {

    //write code here to open a view controller

    let storyboard = "Main"
    let viewControllerIdentifier = "HomeVC"
    let viewController = UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: viewControllerIdentifier) as! HomeVC
    //push/present "viewController"
}

in this code just replace ChatVC name to your viewcontroller name

@objc func goToViewController(_ sender: UIBarButtonItem) {
for controller in self.navigationController!.viewControllers as Array {
                if controller.isKind(of: ChatVC.self) {
                    self.navigationController!.popToViewController(controller, animated: true)
                    break
                } else {
                    self.navigationController?.popViewController(animated: true)
                }
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM