nverinaud / NVSlideMenuController

A slide menu done right for iOS.
MIT License
175 stars 33 forks source link

Properly set parent view controllers on menu and content view controllers on init #30

Closed ajsharp closed 10 years ago

ajsharp commented 10 years ago

When I'm initializing an instance of a NVSlideMenuController and then assigning the menu and content view controllers, I'm getting inconsistent behavior when trying to use the slideMenuController within either of the view controllers, where often the property is nil.

The slideMenuController category method looks for a parentViewController that is an instance of NVSlideMenuController, but the the parent of the menu and content view controllers never gets set to the NVSlideMenuController instance. I think what is needed is just to calladdChildViewController:in thesetContentViewController:andsetMenuViewController:` methods.

Would you accept a patch that makes this change?

nverinaud commented 10 years ago

The view controller hierarchy is done correctly. In which method do you try to access the slideMenuController ?

ajsharp commented 10 years ago

I've tried to access it in various different methods, after the slide menu controller has been allocated and assigned the content and menu controllers:

UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *menuVC = [board instantiateViewControllerWithIdentifier:@"mainSettingsNav"];

SAStoriesCollectionViewController *collectionVC = [[SAStoriesCollectionViewController alloc] initWithNibName:@"SAStoriesCollectionViewController" bundle:nil];
SANavigationViewController *navVC = [[SANavigationViewController alloc] initWithRootViewController:collectionVC];
NVSlideMenuController *slideVC = [[NVSlideMenuController alloc] initWithMenuViewController:menuVC andContentViewController:navVC];
NSLog(@"slide menu controller=%@", navVC.slideMenuController); // slide menu controller=(null)

It's certainly possible I'm doing something wrong, because I've had this work in another view controller class. But I can't get it to work with a navigation controller or any of it's child view controllers.

nverinaud commented 10 years ago

The hierarchy is built when the views appear. The menu's view is lazily created when you first open the menu. Try to log the slideMenuController inside viewWillAppear.

ajsharp commented 10 years ago

Ah, I see. It's probably worth documenting that in the README.

On another note, it would be nice to be able to get access to the slideMenuController in viewDidLoad, as this is guaranteed to run only once for a view controller.

nverinaud commented 10 years ago

My bad. The view controller hierarchy is always built before loading view. So you can access the slideMenuController property in viewDidLoad, viewWillAppear:, etc. like you would with a navigationController or splitViewController. (Proof here and here).

ajsharp commented 10 years ago

Great, thanks for the reference point. I'm not sure why this wasn't getting hit in my code. I'll check again and report back.