lzqok
lzqok

Reputation: 3

Use tabbarViewController.selectedViewContreller didn't push next View

I want to use:

self.tabBarController?.selectedViewController?.navigationController?.pushViewController(vc, animated: false)

but this didn't work in Swift. But it works if I use:

[self.tabBarController.selectedViewController pushViewController:nextVC animated:NO]

in Objective-C. Why?

Upvotes: 0

Views: 65

Answers (2)

user1488696
user1488696

Reputation:

In your objective c example the tabBarController.selectedViewController is assumed to be the UINavigationController instance which is probable correct based on your viewController hierarchy.

However your swift example assumes tabBarController.selectedViewController is embedded in a UINavigationController instance which doesn't match with the objective c version. Accessing the navigationController will return nil because you are asking the navigation controller for its parent navigationController (which doesn't exist).

Your swift example can be fixed like so

guard let navigationController = tabBarController.selectedViewController as? UINavigationController else {
  return
}

navigationController.pushViewController(vc, animated: false)

Upvotes: 0

RajeshKumar R
RajeshKumar R

Reputation: 15778

Try this

tabBarController?.selectedView?.pushViewController(nextVC, animated: false)

Upvotes: 1

Related Questions