Kraishan
Kraishan

Reputation: 455

Switch from UIViewController to other ViewController which is inside navigationController

I have the following views:

TabBarController includes (MapViewController, NavigationController) Navigation Controller includes the DetailViewController which is needed to switch to. Code below is used in the MapViewController.

Right now my code:

    let eventDetailViewController : EventDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("EventDetail") as EventDetailViewController
    eventDetailViewController.event = event
    self.showViewController(eventDetailViewController, sender: self)

This works, but the navigation controller is no where insight (I cannot go to firstviewcontroller for example).

How to fire this code via the navigation controller?

Below is a screenshot of the storyboard. From left to right: TabbarController -> NavigationController and MapController. NavigationController -> EventController. The most righter view is the EventDetailView Storyboard

Upvotes: 0

Views: 281

Answers (1)

pbasdf
pbasdf

Reputation: 21536

So, from your MapView, self.tabBarController will point to the tabBarController. It looks from your storyboard like the Navigation Controller is the first tab, so will be at index 0 in the tabBarController's viewControllers property. So you should be able to do the following:

let eventDetailViewController : EventDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("EventDetail") as EventDetailViewController
eventDetailViewController.event = event
let navController : UINavigationController = self.tabBarController.viewControllers[0] as UINavigationController
navController.pushViewController(eventDetailViewController, animated:YES)
self.tabBarController.selectedIndex = 0

I'm still learning Swift, so you may have to sort some optionals/unwrapping out - but I hope you get the gist.

Upvotes: 1

Related Questions