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?
Asked
Active
Viewed 60 times
0
-
you mean to the previous viewController? – A. Zaima May 10 '17 at 11:25
-
Yes. As in the previous view I was prior to clicking on "Barcode" tab. Example if I was in the "Shopping Cart" and then I went to "Barcode" tab , then once I press the "Back" button it would bring me back to the "Shopping Cart" tab the view that I was before hand. Hope that makes sense. – habed May 10 '17 at 11:27
3 Answers
1
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.

Ivan Ičin
- 9,672
- 5
- 36
- 57
-
-
I believe that you should use a modal View Controller with Done button and then dismiss it and return. – Ivan Ičin May 10 '17 at 11:50
0
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.

A. Zaima
- 144
- 5
0
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
}

Pouya ghasemi
- 231
- 2
- 9