lowtemp
lowtemp

Reputation: 1

Using a button to switch between view controllers on different tabs (self.tabBarController?.selectedIndex = 1 not working)

I'm a bit new to XCode, so I'm not exactly sure what I did wrong. So thank you in advance to anyone who is willing to help me out :)

I have 2+ view controllers imbedded in a tab bar controller, and each vc is imbedded in a navigation controller, and I want to use a button in a vc to switch to another vc in a different tab. Actually, I was able to do this in a past project: I created an IBAction function that switches to a different tab when a button is pressed. Below is what my code looked like (and it worked with no problems at all)

@IBAction func ButtonPressed(_ sender: UIButton) {

    self.tabBarController?.selectedIndex = 1
}

However, it's not working anymore, and I don't know if I did anything differently. Is there a way around this? (Or maybe why this might not be working anymore?)

If that description was a bit confusing, I included a screenshot of what I'm trying to do.

I'm trying to get from the top view controller to the bottom with when the button in the first vc is pressed

Upvotes: 0

Views: 206

Answers (1)

Noah Iarrobino
Noah Iarrobino

Reputation: 1553

if you're in a navigation controller you might need to do this: self.navigationController?.tabBarController?.selectedIndex = 1

Upvotes: 1

Related Questions