UIBARBUTTON back action


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.


navigationController



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.


popViewController


popToRootViewController


viewController


viewController




2 Answers
2



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


#selctor()


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"
}





let viewController = self.storyboard?.instantiateViewController(withIdentifier: "gotoHome") as? HomeTabController. does this code will be effective
– Karthick TM
Jun 30 at 6:41






I didn't understand.
– Mahendra GP
Jun 30 at 6:43





I need the exact code to be written inside the function inorder to open up a view controller @Mahendra GP
– Karthick TM
Jun 30 at 8:15






Haha...exact code...Its a helping community, not paid one...so its better to try something by yourself also.:)
– Mahendra GP
Jun 30 at 8:16



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)
}
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux