1

我正在使用这个 Cocoa Control Side Menu链接

在此菜单中,当您启动应用程序时,您的第一个屏幕有一个导航栏,但是当您打开菜单并按下 TableView 的任何元素时,ViewController 会更改而没有导航栏。我想为 TableView 菜单的每个元素提供一个导航栏,并在导航栏上有一个可以打开和关闭菜单的左按钮。

我使用以下代码启动应用程序:

// 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;
}

在此之后,应用程序从导航栏开始。

但是,在 TableView 如果选择这样的视图控制器,我有导航栏但没有按钮来打开关闭

    - (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;
            }
        }

    }
4

1 回答 1

1

您在加载视图控制器之前添加按钮,您应该在内容视图控制器中加载视图后添加它。

@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];
}

无论如何,我只是添加了一种更轻松地在导航栏上添加菜单按钮的方法。

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

    [self addLeftMenuButtonWithImage:[UIImage imageNamed:@"menu_icon"]];
}
于 2013-09-16T22:18:01.503 回答