iDevelopper / PBRevealViewController

A UIViewController subclass for revealing a left and/or right view controller above or below a main view controller.
MIT License
80 stars 16 forks source link

Why mainViewController is not aded to parentViewController at first call #34

Closed alekseybahtin closed 6 years ago

alekseybahtin commented 7 years ago

I call [self.revealViewController setMainViewController:viewController animated:YES]; at the application start as it is belong on the business logic which controller should be root. But as I see method for adding childViewController: [self addChildViewController:toViewController];

is called only if mainViewController exists: if (_mainViewController && animated) { [self _swapFromViewController:_mainViewController toViewController:mainViewController operation:PBRevealControllerOperationReplaceMainController animated:animated]; }

It brakes my responder chain for actions handling in revealviewcontroller.

iDevelopper commented 7 years ago

Not sure I understand the question. Can you upload her a sample?

abakhtin commented 7 years ago

I added example here https://github.com/abakhtin/pbrevealtest I manually load view controller as main. If I use storyboard segues it works well. Can you help please? Am I misunderstood something about main view controller set method? And the second question what is the difference between push and set segue?

iDevelopper commented 7 years ago

All is in the title: "mainViewController is not aded to parentViewController at first call":

It is the case. You must either use the storyboard with "pb_left" and "pb_main" Set Segues or use the init method programmatically:

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    UIViewController *leftController = [sb instantiateViewControllerWithIdentifier:@"LeftViewController"];
    UIViewController *mainController = [sb instantiateViewControllerWithIdentifier:@"MainViewController"];

    PBRevealViewController *revealController = [[PBRevealViewController alloc]initWithLeftViewController:leftController mainViewController:mainController rightViewController:nil];

    self.window.rootViewController = revealController;

In a storyboard, the PBRevealViewControllerSegueSetController segue is for setting a controller (the equivalent of the root view controller segue for a navigation controller for example). The PBRevealViewControllerSeguePushController segue is for pushing a controller (the equivalent of the Show segue for a navigation controller).

abakhtin commented 7 years ago

Clear. The problem is that I want to load reveal from storyboard but set up main in code based on the flow. Why you need this crazy workaround with checking for existing controller: if (_mainViewController && animated) { [self _swapFromViewController:_mainViewController toViewController:mainViewController operation:PBRevealControllerOperationReplaceMainController animated:animated]; } Can it be improved? Or should I create revealVC in code? :)

iDevelopper commented 7 years ago

What is the reason you want to do that?

abakhtin commented 7 years ago

I have different controllers that can be used as main at application start based on the business logic. But left controller is always the same and I want them to be together in single storyboard. That it is a very rare case, but I think you can add assert about it so developers can understand why it is not working.

iDevelopper commented 7 years ago

Ok try with this version. I will update the Swift version too and push a commit but not today...

pbrevealtest-master.zip

abakhtin commented 7 years ago

great thanks i ll check when it will be available in the repo.

iDevelopper commented 7 years ago

Done! Github repo here and Cocoapods here. On Cocoapods there may be a delay.

Oleygen commented 5 years ago

When working with pure programmatically UI: Still having issue with this. All flow just breaks if setMainViewController function is called before PBReaviewViewController.loadView() method.

iDevelopper commented 5 years ago

You have to create PBRevealViewController object first.