0

I have to change my root view controller according to login status.

I had change my AppDelegate file and create a MainNavigationController class which extends UINavigationController.

**PROBLEM :- ** When the defined root class is loaded it gives all outlet variables as nil.

CODE

 class PostalViewController: UIViewController {

        @IBOutlet weak var btn_currentLocation: UIButton!
        @IBOutlet weak var btn_viewAccount: UIButton!
        @IBOutlet weak var in_postCode: UITextField!

        let locManager = CLLocationManager()

        override func viewDidLoad() {
            super.viewDidLoad()

// Here it gives a nil on btn_currentLocation

            btn_currentLocation.layer.cornerRadius = 15.0
            btn_currentLocation.layer.masksToBounds = true

            btn_currentLocation.setButtonGradient(colorOne: UIColor(named: "lightBlue")!, colorTwo: UIColor(named: "gradient2")!)

            btn_viewAccount.layer.cornerRadius = 15.0
            btn_viewAccount.layer.masksToBounds = true

            // Add search button in post code text field
            let searchButton = UIButton(type: .custom)
            searchButton.setImage(UIImage(named: "iconSearch"), for: .normal)
            searchButton.frame = CGRect(x: CGFloat(in_postCode.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
            searchButton.addTarget(self, action: #selector(self.actionSearch), for: .touchUpInside)
            in_postCode.rightView = searchButton
            in_postCode.rightViewMode = .always

        }

AppDelegate.swift (only that particular function)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = MainNavigationController() 
        return true
    }

MainNavigationController.swift

class MainNavigationController : UINavigationController{

    var isLogin : Bool?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        isLogin = UserDefaults.standard.bool(forKey: "isLogin")
        print("IsLogin --->", isLogin!)

        if (isLogin != nil && isLogin!){

            perform(#selector(postalController), with: nil, afterDelay: 0.01)

        }else{

            perform(#selector(showHomeContoller), with: nil, afterDelay: 0.01)
        }

    }

    @objc func showHomeContoller () {

        let homepageController = HomePageViewController()
        present(homepageController, animated: true, completion: nil)
    }

    @objc func postalController () {

        let postalController = PostalViewController()
        viewControllers = [postalController]
    }
}

EDIT

I have add a screenshot below, I have to switch my root view between HomePage and Postal

enter image description here

Nikhil Sawant
  • 103
  • 10

2 Answers2

1

I suppose you just want to show PostalViewController if user is logged. If user isn't logged you want to show just HomePageViewController. First delete these lines from app delegate, you don't need this:

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationController() 

Now delete the whole navigation controller class. You won't need it because you will do all of this in HomePageViewController. Now in HomePageViewController swift file add this to viewDidLoad()

isLogin = UserDefaults.standard.bool(forKey: "isLogin")
    print("IsLogin --->", isLogin!)

if isLogin != nil {
    performSegue(withIdentifier: "segueToPostal", sender: self)
}

In the end set segue from HomePageViewController to PostalViewController

enter image description here enter image description here

and set its identifier as segueToPostal

enter image description here

If you don't want to let user navigate back from Postal view controller you can just embbed PostalViewController in new NavigationController. Then just set segue from HomePageViewController to this NavigationController and set identifier.

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • Yes, I have the tried the same, still not working, it gives the same runtime error – Nikhil Sawant Nov 12 '18 at 15:38
  • Thanks for effort, intially i have implemented this, but here the problem is on Postal screen it gives me the back button of Homepage. And if i perform segue as present, then further back navigation is not showing. – Nikhil Sawant Nov 12 '18 at 16:33
  • @NikhilSawant Don't perform segue as present, try what I wrote: performSegue(withIdentifier: "segueToPostal", sender: self) and set segue how I described – Robert Dresler Nov 12 '18 at 16:37
  • yes i have tried the same way you mentioned. Problem is on Postal screen it's giving a back button to HomePage again, which i don't want to. – Nikhil Sawant Nov 12 '18 at 16:39
  • Thanks Robert for your valuable time. We have solved it. I just apply your method and hidden the back button on Postal screen. Thanks a lot. – Nikhil Sawant Nov 12 '18 at 16:45
  • @NikhilSawant ok, you can also try what I add at the end of my answer – Robert Dresler Nov 12 '18 at 16:45
1

IBOulet is set when view controller is initialized with nib file. So you need create the view controllers using the init method below

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)

For example, if name of your xib file where you config your IBOultets the same as controllers name, you can just do next:

let postalController = PostalViewController(nibName: "PostalViewController", bundle: nil) 

Here You will have controller with initialized IBOutlets.

For using story board you should init them from story board. Try to ini controllers next way:

let sb = UIStoryboard(name: "MainStoryboard", bundle: nil) 
let vc = sb.instantiateViewController(withIdentifier: "PostalViewController") 

And you should set storyboard id in your story board for the controller, like on the screen below.

storyboard id

Also you can find more info how to init correctly view controllers from storyboard here

What is a StoryBoard ID and how can i use this?

Gkolunia
  • 92
  • 9
  • Thanks for your response, but still the same results. i have replace in MainNavigationController – Nikhil Sawant Nov 12 '18 at 15:50
  • Sorry, I didn't notice that you use story board, try to create PostalViewController with next code let sb = UIStoryboard(name: "MainStoryboard", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "PostalViewController") if you set storyboard id for the controller. – Gkolunia Nov 12 '18 at 15:57
  • let me try it. Thanks – Nikhil Sawant Nov 12 '18 at 16:05
  • please, check out my edited post, i've added more information. – Gkolunia Nov 12 '18 at 16:11
  • @NikhilSawant and Have you used the id for create the view controller from story board, how I do in comment above? – Gkolunia Nov 12 '18 at 16:21
  • Yes, but i have not used xib file anywhere rather i don't know the implementation from xib procedure. – Nikhil Sawant Nov 12 '18 at 16:35
  • The last procedure is based on storyboard, you should use storyboard id to create the view controller in the way. let sb = UIStoryboard(name: "MainStoryboard", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "PostalViewController") – Gkolunia Nov 12 '18 at 16:38
  • Thanks a alot man for your valuable time. I have used the above answer. I appreciate your efforts. – Nikhil Sawant Nov 12 '18 at 16:48