Stephy Samaniego
Stephy Samaniego

Reputation: 242

Initial view controllers in Swift

I am new programming in Swift but i`m having some problems.

I have a login page (LoginViewController) and when i click the button it heads to another View Controller (MainViewController). The problem is that in MainViewController i'm using a Slide Menu from here: https://github.com/dekatotoro/SlideMenuControllerSwift and the code where the slide menu is done is in AppDelegate:

private func createMenuView() {

    // create viewController code...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)


    let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
    let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
    let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController

    let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)

    UINavigationBar.appearance().tintColor = UIColor(hex: "689F38")

    leftViewController.mainViewController = nvc

    let slideMenuController = ExSlideMenuController(mainViewController:nvc, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
    slideMenuController.automaticallyAdjustsScrollViewInsets = true
    self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
    self.window?.rootViewController = slideMenuController
    self.window?.makeKeyAndVisible()


}

and in this application function the createMenuView is called:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.createMenuView()
    return true
}

the problem with this is that LoginViewController is the Initial View Controller, and when i leave it like that the MainViewController appears as the Initial View Controller.

How can i do so that the LoginViewController is the initial and when the user press the button in login set to MainViewController with all the menu thing.

Please help me i really don`t know what to do :(

Upvotes: 2

Views: 744

Answers (1)

Sankar Narayanan
Sankar Narayanan

Reputation: 119

It is because you're replacing root view controller to be slideMenuController in createMenuView method and calling it in didFinishLaunchingWithOptions in app delegate. This code will get executed as soon as the app is launched.

Solution: Instead of doing this, try to get the application delegate instance using UIApplication.sharedApplication().delegate in createMenuView method, define this method in LoginViewController and then call this method after button click in LoginViewController (where ever you wanted to call). Please call createMenuView() method after button click in login view controller, may be in ViewWillAppear of mainViewController.

Rewrite the method like this inside LoginViewController or where ever you want it to be based on your requirement:

private func createMenuView() {
    // create viewController code...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
    let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
    let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
    let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
    UINavigationBar.appearance().tintColor = UIColor(hex: "689F38")
    leftViewController.mainViewController = nvc
    let slideMenuController = ExSlideMenuController(mainViewController:nvc, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
    slideMenuController.automaticallyAdjustsScrollViewInsets = true
    let appDelegate = UIApplication.sharedApplication().delegate
    appDelegate.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
    appDelegate.window?.rootViewController = slideMenuController
    appDelegate.window?.makeKeyAndVisible()
}

Call this method on the @IBAction of the login button in LoginViewController.

I hope this answers you're question.

Upvotes: 3

Related Questions