0

I'm new in Swift and I've this issue: I load the main storyboard and if the user is nil then I load another storyboard with other scenes. For doing this I run this code:

override func viewDidAppear(_ animated: Bool) {
    if (true) {
        let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController
        loginScene.modalPresentationStyle = .fullScreen
        show(loginScene, sender: nil)
    }
}

I know that the if statement is always true, I want it at the moment. I also checked if the login scene was the initial one, but even like this the navigation bar doesn't show itself. The problem is that now ini the login scene I don't see the navigation bar and I don't get why. What can I do? If you need more explanations I'll give you. Thanks!

Elia Mirafiori
  • 107
  • 2
  • 8
  • you dont see nav bar for modal presentation, you need to push the controller. – Teja Nandamuri Aug 11 '20 at 13:08
  • @TejaNandamuri I changed my code like this: self.view.window?.rootViewController = loginScene but the problem persists. I've also tried this: I've also tried this: self.navigationController?.pushViewController (loginScene, animated: true) – Elia Mirafiori Aug 11 '20 at 13:19

2 Answers2

1

As far as I understand

  1. your main storyboard has no NavigationController therefore:
self.navigationController?.pushViewController(loginScene, animated:true)

won't work since self.navigationController? is nil

  1. Your NilUser Storyboard seems to have the loginScene (UIViewController) embedded in a UINavigationController I guess?!? and you expect this UINavigationController to be presented

by calling

let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController

you instantiate your LoginController directly without it's UINavigationController therefore you should make the UINavigationController the initial ViewController and try

let storyboard = UIStoryboard(name: "NilUser", bundle: nil)
guard let navigationController = storyboard.instantiateInitialViewController() else {
    // initial UINavigationController not found
    return
}
navigationController.modalPresentationStyle = .fullScreen
show(navigationController, sender: nil)

If I'm wrong I might have missed something in your setup.

Daniel Marx
  • 676
  • 5
  • 8
  • Hi @danielmarx I've just checked and you're right, my navigation controller is nil... So now how can I solve this issue? How can I make the UINavigationController the initial view controller programmatically and by following the scheme "Main" -> "login"? – Elia Mirafiori Aug 11 '20 at 14:49
  • This depends on what you want to achieve. First of all: Is there a UINavigationController on your MainStoryboard which embeds your 'let's call it' MainViewController? If not you should add one if you want one. I guess you want to check if a user is logged in and if not present/push the UserNil Storyboard. And you should be familiar with different presentation styles. You can call present on any UIViewController but only push on a UINavigationController. show(...) decides for you wether to push or present. Maybe checkout this link: https://stackoverflow.com/a/25967267/4190135. – Daniel Marx Aug 11 '20 at 15:01
  • Exactly: I've the main storyboard for the user when logged in, it's a Tab Bar Controller, with it there is also a SideMenu Controller. Now I'm developing the part where the user isn't logged in and I thought I could load another storyboard, and so I did. The NilUser storyboard has a navigation controller to manage the scenes, but as I told you the navigation bar doesn't appear. I'm new in swift and I've always code in Java for Android, so I wonder know if there is a method that allows me to load another storyboard without inheriting or being constrained by the previous storyboard. – Elia Mirafiori Aug 11 '20 at 16:08
  • as I said ```UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController``` will instantiate the LoginViewController from the storyboard without the UINavigationController on this storyboard. Open your UserNil storyboard and select the UINavigationController. Make sure to check "is Initial View Controller" in the attribute inspector. and use the ```storyboard.instantiateInitialViewController()```to initialise it. then present it with ```show(, sender nil)``` – Daniel Marx Aug 12 '20 at 07:11
  • you also can try ```let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController; let nav = UINavigationController(rootViewController: loginScene); show(nav, sender: nil)``` to create a UINavigationController programatically – Daniel Marx Aug 12 '20 at 07:14
0

try this

let loginScene = UIStoryboard(name: "NilUser", bundle: nil).instantiateViewController(withIdentifier: "loginScene") as! LoginController

   self.navigationController?.pushViewController(loginScene, animated:true)
  • Hi @shanthkumar I've already tried this, and to be sure I tried again now, but it doesn't work. When I use pushViewController method it remains in the scene where I invoke the method, so this one doesn't even load the login scene – Elia Mirafiori Aug 11 '20 at 13:27