0

I have an app that consists of a sign up and login view controller. Once users have signed up or logged in, they are segued to the main view controller. I've implemented NSUserDefaults to store a boolean value of the users login status, so that when they reopen the app they are automatically segued to the main view controller rather than having to go through the login view controller. I'm checking the users login status in didFinishLaunchingWithOptions and if the login status stored in NSUserDefaults is true, I initiate the segue to the main vc.

I'm currently using the code below to present the main view controller:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"Recorder") as! RecordController
let navController = UINavigationController.init(rootViewController: viewController)

if let window = self.window, let rootViewController = window.rootViewController 
{
    var currentController = rootViewController
    while let presentedController = currentController.presentedViewController 
    {
        currentController = presentedController
    }
    currentController.present(navController, animated: true, completion: nil)
}

The problem with this is that I don't want my main view controller to be my root view controller and presenting the main view controller is creating some weird vertical spacing that I can't figure out - might be some issue with the navigation controller.

Brosef
  • 2,945
  • 6
  • 34
  • 69
  • Possible duplicate of [Segue from LaunchScreen to a viewController in Main.Storyboard?](https://stackoverflow.com/questions/39231395/segue-from-launchscreen-to-a-viewcontroller-in-main-storyboard) – Shades Aug 25 '17 at 20:07
  • @Shades I tried that solution. The problem is that I have a navigation controller and when I land on my main view controller my app crashes because of this line `self.navigationController!.navigationBar.barTintColor = UIColor.white` – Brosef Aug 25 '17 at 20:09
  • Give the nav controller a storyboard ID and instantiate it directly – Shades Aug 25 '17 at 20:13
  • @Shades tried that also. My IBOutlets are now failing in my main view controller. – Brosef Aug 25 '17 at 20:23
  • I'm not sure what you mean by this line "I don't want my main view controller to be my root view controller" is this because you want your navigation controller to be your root view controller? – allenh Aug 25 '17 at 20:26
  • @AllenHumphreys I dont want my main view controller to be my root, because if the user signs out, they pop back to the root controller which is the login controller `_ = self.navigationController?.popToRootViewController(animated: true)` – Brosef Aug 25 '17 at 20:28
  • You can't do that with "present", you'll need to push your view controller onto the navigation stack that already exists. Presumably, your login view controller is contained inside of a navigation controller already, and that navigation controller should root view controller of the main window. Show just get it, and push it on without animations. And obviously, don't put your Recorder vc into a nav controller if you go that route. – allenh Aug 25 '17 at 20:31

1 Answers1

0

Based on a clarification, here is what I think you should be doing. What you should have at a clean launch is a UINavigationController with the log in view controller as it's root. In the case you launch when you're logged in, grab the navigation controller and just push your other view controller onto the stack. This will be effectively, from UIKit's perspective, be the same thing as actually following that show segue after a log in. Show segues are adaptive. They get turned into navigation pushes if the source is in a navigation controller and a present if it is not.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let loggedIn = true

    if loggedIn, let navigationController = window?.rootViewController as? UINavigationController {

        let newVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Recorder")

        navigationController.pushViewController(newVC, animated: false)
    } else {
        print("expectation failed")
    }

    return true
}

enter image description here

allenh
  • 6,582
  • 2
  • 24
  • 40
  • I implemented your approach, but I noticed that my tableview is being pushed down 64 pixels which is the same height as the navigation bar...Do you think by instantiating the navigation controller manually there is an extra 64px being added? – Brosef Aug 26 '17 at 00:54
  • @Brosef Which vc has the table view, and is it a UITableViewController or is it just a table view inside a regular view controller? – allenh Aug 26 '17 at 00:57
  • Its the view controller after the main view controller (Recorder). Its just a `UITableView` placed within the view controller. – Brosef Aug 26 '17 at 00:58
  • I'm sorry, I can't reproduce it in my simplified example. More than likely, one of your view controllers needs to have the "Adjust Scroll View Insets" turned off it's under View>Utilities>Show Attributes Inspector – allenh Aug 26 '17 at 01:13
  • Here's a clue. Prior to instantiating the navigationController in `didFinishLaunchingWithOptions`, I set the color of my navigation bar to white in my main view controller and every view controller I segued to from the main view controller had the white navigation bar. Now, every view controller I segue to has the default gray navigation bar color, even though the navigation bar is white in the main view controller.. – Brosef Aug 26 '17 at 01:25
  • That sounds like you have 2 navigation controllers, you should only have one. Are you should you're not pushing a navigation controller onto the first navigation controller? – allenh Aug 26 '17 at 01:39
  • I have the navigation controller I've always had that I dropped into story board. And now your implementation by doing `let navigationController = window?.rootViewController as? UINavigationController` in `didFinishLaunchingWithOptions` – Brosef Aug 26 '17 at 01:44
  • That's not a new one, that's just getting the current one by casting it. If you can show more details I can help more. Screenshots of storyboards and more code. – allenh Aug 26 '17 at 01:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152878/discussion-between-brosef-and-allen-humphreys). – Brosef Aug 26 '17 at 04:14
  • @Brosef I've added some things to the chat, I'll check back later today if you have any follow up. – allenh Aug 26 '17 at 14:32