Reputation: 198
I'm writing an app with Swift in Xcode, and I'm struggling with 1 thing.
I have a UITabBarController. There are 2 tabs in it, let's say "Home" and "News".
Now, here comes my issue. In Home, I display the most recent news, and only this one. Right now, if I tap this News, I segue to the NewsDetails view controller, BUT I'm staying in the Home tab and its own Navigation Controller. What I'd like to do, is to segue to the NewsDetails, but within the News Tab and within the News Navigation Controller.
To summarize, the behaviour I'm looking for goes as followed: when I click on the most recent news in the Home page, I want to end up within the News tab, looking at the news details. And from there, if I tap the back button, I'm going back to the News tab root, not the Home tab.
I hope I managed to make myself clear enough. Any help, guidance or lead would be much appreciated.
Thank you!
Upvotes: 0
Views: 39
Reputation: 2678
if let tabVC = self.tabController {
tabVC.selectedIndex = 1
if let navController = tabVC.selectedViewController as? UINavigationController {
let vc = UIViewController()
navController.pushViewController(vc, animated: false)
}
}
Since you mentioned there are only 2 tabs, so I assume the News is the second tab and hence given the selectedIndex as 1
Also since you are calling this from the Home view controller, we can directly fetch the tab bar using self.tabController. Alternatively you can also use -
let tabVC = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController
Upvotes: 1