简体   繁体   中英

MVYSideMenu using Navigation Controller in all view controllers iOS

I am using this Cocoa Control Side Menu link .

In this Menu, when you start the app you have a Navigation Bar for the first screen, but when you open the menu and press any element of TableView, the ViewController change without Navigation Bar. I want to have a Navigation Bar for each of the elements of the TableView Menu and a left button on the Navigation bar that could open and close the Menu.

I start the application with this code:

// appDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    YPMenuViewController *menuVC = [[YPMenuViewController alloc] init];
    YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
    UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];

    YPSideMenuOptions *options = [[YPSideMenuOptions alloc] init];
    YPSideMenuController *sideMenuController = [[YPSideMenuController alloc] initWithMenuViewController:menuVC
                                                                                    contentViewController:contentNavigationController
                                                                                                  options:options];
    self.window.rootViewController = sideMenuController;    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

After this, the app starts with Navigation bar.

But then, in the TableView If Select the view controllers like this, I have the Navigation Bar but without button to open an close

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        switch (indexPath.row) {
            case 0:
            {
                YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
                UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];

                UIButton *iwantView = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
                [iwantView setBackgroundColor:[UIColor blackColor]];
                [iwantView addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];

                contentNavigationController.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:iwantView];
                [[self sideMenuController] changeContentViewController:contentNavigationController closeMenu:YES];
                break;
            }
         case 1:
              {
           YPProjectListViewController *contentVC = [[YPProjectListViewController alloc] init];
           UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
            [[self sideMenuController] changeContentViewController:contentNavigationController closeMenu:YES];
            break;
              }

            default:{
                YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
                [[self sideMenuController] changeContentViewController:contentVC closeMenu:YES];
                break;
            }
        }

    }

You are adding the button before the view controller is loaded, you should add it after loading the view in your content view controller.

@implementation YPProfileViewController
---------------

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UIButton *iwantView = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
    [iwantView setBackgroundColor:[UIColor blackColor]];
    [iwantView addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:iwantView];
}

Anyway I just added a method to add a menu button on the navigation bar more easily.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self addLeftMenuButtonWithImage:[UIImage imageNamed:@"menu_icon"]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM