【问题标题】:MVYSideMenu using Navigation Controller in all view controllers iOSMVYSideMenu 在所有视图控制器 iOS 中使用导航控制器
【发布时间】:2013-09-10 14:33:42
【问题描述】:

我正在使用这个 Cocoa Control Side Menu link

在此菜单中,当您启动应用程序时,您的第一个屏幕会有一个导航栏,但是当您打开菜单并按下 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;
            }
        }

    }

【问题讨论】:

    标签: ios objective-c iphone uitableview mvysidemenu


    【解决方案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"]];
    }
    

    【讨论】:

    • 感谢您的解决方案。我有最后一个问题,请问是否可以禁用某些视图控制器的侧视图控制器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多