Reputation: 201
In the below figure, I have a "Back" button in the nav bar which I would want for it to close the Barcode Scanner tab and bring me to to the view I was prior to hitting the "Back button. How would that be possible?
Upvotes: 0
Views: 62
Reputation: 241
you can override back button in ViewDidLoad like :
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(self.back(_:)))
}
func back(_ sender: AnyObject) {
//if you want to go to previous view use this code
self.navigationController?.popViewController(animated: true)
//if you want to go to a tab bar view use this code
//index of your tab bar
tabBarController?.selectedIndex = 1
}
Upvotes: 0
Reputation: 144
View controllers that you load are all stacked in order of you viewing them. To go back to your previous view simply dismiss the last view controller. You can use this code:
@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
Hopefully this should do the trick.
Upvotes: 0
Reputation: 9990
Your request is improper UI. Tabs should not contain back button for going to previous tab, and I really hope that 'Close' the tab doesn't mean you want to remove it.
Other than that you can change active tab UITabBar.setSelectedItem. But really in your case don't do it.
Upvotes: 1