NilsBerni NilsBerni - 3 months ago 13
iOS Question

TabBar icon two selected in viewDidLoad in Swift 2?

i want to open my UITabBarController using the Main.storyboard like in the image below. The problem is that i present a different ViewController modally from the Second View and i want to go back from this ViewController to my previous ViewController. How could i load my UITabBarController with the Second icon selected? is there any code for the viewDidLoad you guys know?

I also thought about an Unwind Segue but everything i try doesn't work.

optimally i would like to restore my selected tab bar icon.

enter image description here

Thanks in advance!

Answer

Right now you seem to have a segue from "Second View" to the "Tab Bar Controller" and it always leads to "Item 1".

Ok... finally the right answer:

1) Modal segue from "Second View" to VC

2) IBAction from button in VC with the following code:

 @IBAction func test(_ sender: AnyObject) {
    self.dismiss(animated: true, completion: {})
}

The idea is simple: use a modal segue to pop the VC and then just dismiss it instead of using a segue, which always hides the tab bar afterwards.

enter image description here