0

I am going to do my best to explain my problem, so thanks in advance.

I usually build applications with many view controllers by having each ViewController represent a different view. Right now, I am creating a large application with many different views. I do not want to have 15+ ViewControllers in my application. I would rather have one ViewController inherit different UIViews.

For example, if on my menu bar I select the "Profile" option. I would then be taken to the selectionController and have the selectionController inherit the UIView named Settings View

For some reason upon calling the function to display the profileView nothing happens. I have implemented a similar feature like this before but not it isn't working.

HomeController

//This function is called upon menu tap

func displayController(menuOption: MenuOption, view: UIView) {
    let selectionController = UIViewController()
    selectionController.view.frame = UIScreen.main.bounds
    selectionController.navigationItem.title = menuOption.name
    selectionController.view.backgroundColor = Color.defaultBackgrondColor

    selectionController.view.addSubview(view)

    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.pushViewController(selectionController, animated: true)
}

Menu Bar

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

//CARRY OUT ANIMATION FOR MENU BAR...

    }) { (completed: Bool) in
        let option = self.menuOptions[indexPath.row]

        switch option.name {
        case "Profile":

//This is where I call my function to display the new view

            self.homeController?.displayController(menuOption: option, view: ProfileView(frame: UIScreen.main.bounds))
        default:
            print("Staying on home page")
        }
    }
}
Chandler Long
  • 77
  • 2
  • 11
  • Make sure `self.homeController` isn't nil. If that's not the problem, make sure `homeController` is actually in a navigation controller. – rmaddy Jun 19 '18 at 18:54
  • Home controller does come up nil when I add a breakpoint. What would the steps be to initialize the home controller? – Chandler Long Jun 19 '18 at 19:08

1 Answers1

0

Have you ensured that the navigation controller has been initialized? Are you setting up your home controller using storyboards or programmatically?

If you place a breakpoint at the line in question it should give you more information on whether it has been initialized or not.

rob
  • 99
  • 1
  • 3
  • At what point are you initializing the HomeController? I'm assuming you've placed the breakpoint at this line here?: self.homeController?.displayController(menuOption: option, view: ProfileView(frame: UIScreen.main.bounds)) The home controller property is an optional in this case. When an optional property is nil, whatever statement after is not executed as otherwise it would cause a crash. – rob Jun 19 '18 at 19:17
  • I have the homeController laid out as _var homeController: HomeController?_. If I were to initialize it as _let homeController = HomeController()_ it will causes a crash upon launch. – Chandler Long Jun 19 '18 at 19:22
  • I have tried adding _menuLauncher.homeController = self_ in the function in the _HomeController_ with no luck. – Chandler Long Jun 19 '18 at 19:23
  • I'm assuming MenuLauncher is the View Controller containing the collection view code? Could you assign and create the home controller just before you perform the call on it like so? `self.homeController = HomeController() self.homeController?.displayController(menuOption: option, view: ProfileView(frame: UIScreen.main.bounds))` Also I'm slightly confused, as (from my understanding) you're essentially creating the home controller just to present the SelectionController? – rob Jun 19 '18 at 19:50
  • Right. So I have a home controller that displays the homeView. But I have a slide out menu that features a collectionView with options to choose from. Once the index is selected then the menuLauncher calls the function from the homeController. – Chandler Long Jun 19 '18 at 21:00