我正在使用此Cocoa控制侧菜单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;
}
}
}
答案 0 :(得分: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"]];
}