3

I'm trying to push a ViewController programmatically.

Code:

var plus = UIButton()

plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)

@objc func plusPressed() {
    print("plus")
    let createJournalVC = CreateJournalViewController()
    self.navigationController?.pushViewController(createJournalVC, animated: true)
}

What works:

  1. Once the button is pressed, "plus" is printed to the console.

What doesn't work:

  1. The ViewController is not pushed.

Details

  • I am using a Navigation Controller & Tab Bar Controller.
  • I am making this only programmatic, no storyboards.
  • There is no error printed to the console, nothing actually happens.
Cal
  • 422
  • 6
  • 20
  • 1
    see this for help : https://stackoverflow.com/questions/39929592/how-to-push-and-present-to-uiviewcontroller-programmatically-without-segue-in-io/39929636#39929636 – Anbu.Karthik Nov 19 '18 at 09:26
  • Check if the navigationController exists in the `plusPressed` method. – Rakesha Shastri Nov 19 '18 at 09:27
  • @Anbu.karthik But how come I don't have this issue in any of my other projects? – Cal Nov 19 '18 at 09:28
  • @RakeshaShastri expand please, sorry, I'm new. – Cal Nov 19 '18 at 09:28
  • set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of `navigationController` – Rakesha Shastri Nov 19 '18 at 09:29
  • @VersionSwift Please check your navigationController is not nil, As you are using tabbarcontroller – iOS_Maccus Nov 19 '18 at 09:35
  • self.navigationController must be returning nil that is why you don't see the view controller being pushed. The view controller (self) should have been embedded in a navigation controller. – prex Nov 19 '18 at 09:39
  • if tabbarcontroller comes after navigationcontroller then navigationcontroller can become nil.... You should rather put tabbarcontroller first and then put each viewcontroller (that are related to each tab) into there own navigaitoncontroller – N4SK Nov 19 '18 at 09:40

2 Answers2

2

If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.

Storyboard:

Each Tab's ViewController gets their own NavigationController

Programmatically:

You need to create your TabBarController like this...

window = UIWindow(frame: UIScreen.main.bounds)
let tabCon = UITabBarController()
let navCon1 = UINavigationController(rootViewController: ViewController())
let navCon2 = UINavigationController(rootViewController: CreateJournalViewController())
let navCon3 = UINavigationController(rootViewController: AnotherViewController())
tabCon.viewControllers = [navCon1, navCon2, navCon3]
tabCon.tabBar.items?[0].title = NSLocalizedString("VC", comment: "comment")
tabCon.tabBar.items?[1].title = NSLocalizedString("CJV", comment: "comment")
tabCon.tabBar.items?[2].title = NSLocalizedString("AVC", comment: "comment")
window?.rootViewController = tabCon
window?.makeKeyAndVisible()
N4SK
  • 700
  • 8
  • 25
  • Seems to work, but I can't assign any properties to each TabBarItem. – Cal Nov 20 '18 at 07:56
  • You get the array of each tabBarItem from the following code: `tabCon.tabBar.items` refer to this link: https://stackoverflow.com/a/12578423/4566190 If this works for you please mark this as correct answer... thanks – N4SK Nov 20 '18 at 16:47
  • I did this in my app delegate but still nothing happens - https://pastebin.com/RSKFWCaq – Cal Nov 21 '18 at 00:50
  • Please make sure that you have set the `backgroundColor` of views of all the controllers, and you have added only one tab in your `tabCon.viewControllers` array, you have only added `navCon1`, although I tested your code on my Mac and it seems to work fine for me, I am getting a title on each tab. I'll update my code up in the answer... If you still face the issue you can search the stack-overflow – N4SK Nov 21 '18 at 02:04
  • If you still face the issue you can search the stack-overflow... give more details about what is actually happening on screen and what were you expecting instead – N4SK Nov 21 '18 at 02:10
  • See, your code works. But then I don't get a title for my NavigationBar because I can't do `self.title = "Home"` in each ViewController as that will override my title for the TabBarItem aswell. – Cal Nov 21 '18 at 02:16
  • If you know how to fix that, I'd be more than happy to give your answer a tick. – Cal Nov 21 '18 at 02:16
  • Instead of using `self.title` I believe you should use this in viewDidLoad: https://stackoverflow.com/a/21615726 – N4SK Nov 21 '18 at 02:57
0

It seems navigationController is nil, that's why the view controller is not pushed.

Vladimir Kaltyrin
  • 621
  • 1
  • 4
  • 16