Using pkrevealcontroller on existing Xcode storybo

2019-03-21 23:49发布

问题:

I am trying to integrate the pkrevealcontroller sliding menu into an existing iOS project that has an existing storyboard with segues, etc. I extended UINavigationViewController and linked my new class to the Nav Controller on the storyboard. In my app delegate I do the following:

MainNavViewController *frontViewController = [[MainNavViewController alloc] initWithRootViewController:[[myRootViewController alloc] init]];
UIViewController *rightViewController = [[menuViewController alloc] init];

self.revealController = [PKRevealController  revealControllerWithFrontViewController:frontViewController
                                                                rightViewController:rightViewController
                                                                            options:nil];

self.window.rootViewController = self.revealController;

When I run the application, it successfully adds the sliding menu icon to the navigation bar, and the frontview slides as I want it. But it is not using the title or segues I have added on the storyboard. Is what I'm trying even possible.

回答1:

I think the problem is that you're instantiating a new frontViewController -- that's not the one that's in your storyboard. I'm not exactly sure how to do this, but I would try it like this. I would add a UIViewController in the storyboard -- change its class to PKRevealController, and make it the initial controller in the storyboard, but don't hook it up to the rest of the scenes. Give your MainNavViewController an identifier in IB, then change the code in your app delegate to this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.revealController = (PKRevealController *)self.window.rootViewController;
    UIViewController *rightViewController = [[menuViewController alloc] init];
    MainNavViewController *frontViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"frontViewController"];
    [self.revealController setFrontViewController:frontViewController];
    [self.revealController setRightViewController:rightViewController];
    return YES;
}


回答2:

My app delegate does not have the revealController variable so had to create that manual, is that something you have to do?



回答3:

In Swift use this,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    var frontViewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("frontVC") as! UIViewController

    var leftViewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("leftVC") as! UIViewController

    var revealController:PKRevealController = PKRevealController(frontViewController: frontViewController, leftViewController: leftViewController)

    self.window?.rootViewController = revealController

    return true
}