我在故事板中創建了幾個ViewControllers,每個ViewController都有自己的類文件。在AppDelegate中,我編程生成了一個UINavigationController,它存在於每個頁面的應用程序頂部。這將有兩個按鈕對每個ViewController都是相同的,一個將加載一個名爲'settings'的ViewController,然後一個會觸發一個顯示側面菜單的方法。從UINavigationController執行ViewController的方法
屏幕截圖來說明:
目前,每個視圖控制器在左上角的按鈕時,按下移動時穿過露出下面的菜單當前視圖控制器。
這工作正常,但我想要的是這個按鈕被刪除,並用導航控制器上的按鈕(在紫色NavigationController中看到的當前佔位符菜單按鈕)替換。
如何實現在AppDelegate中移動ViewController的代碼,生成了什麼UINavigationController?
AppDelegate.m
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[mainVC setTitle:@"Congress app"];
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];
mainVC.navigationItem.leftBarButtonItem = showMenuButton;
mainVC.navigationItem.rightBarButtonItem = showSettingsButton;
[self.window setRootViewController:navVC];
[_window makeKeyAndVisible];
return YES;
}
- (IBAction)revealMenu:(id)sender
{
// This obviously won't work but what should go here instead?
// Something like get instance of MainViewController and fire it's reveal menu
// method but passing the current ViewController Id and running slidingViewController anchorTopViewTo:ECRight on that?
[self.slidingViewController anchorTopViewTo:ECRight];
}
MainViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MenuVC"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_menuBtn.frame = CGRectMake(8, 80, 34, 24);
[_menuBtn setBackgroundImage:[UIImage imageNamed:@"menuButton.png"] forState:UIControlStateNormal];
[_menuBtn addTarget:self action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.menuBtn];
NSLog(@"MainVC loaded");
}
- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}
我明白你的意思,但這似乎是一個這樣簡單的事情來實現 - 一個按鈕,採取當前ViewController並運行它的一個方法。我想我錯過了一些相當基礎的東西。 – Ryan
並非如此,使用像https://github.com/ECSlidingViewController/ECSlidingViewController這樣的第三方控制器可以節省您的時間並獲得更好的應用程序設計。你不應該在每個視圖控制器的幻燈片功能... – Wain
我實際上使用該第三方控制器(ECSlidingViewController)。它附帶的教程實際上顯示了每個VC中使用的滑塊功能。我添加了NavigationController作爲整個應用程序的靜態菜單。 – Ryan