从UINavigationController执行ViewController的方法

时间:2014-01-17 12:34:48

标签: objective-c cocoa-touch ios7 uinavigationcontroller

我在故事板中创建了几个ViewControllers,每个都有自己的类文件。在AppDelegate中,我以编程方式生成了一个UINavigationController,它存在于每个页面的应用程序顶部。这将有两个按钮对于每个ViewController都是相同的,一个将加载一个名为'settings'的ViewController,另一个将触发一个显示侧边菜单的方法。

屏幕截图说明:

Button to reveal the menu underneath Current ViewController slid across to reveal the menu

目前,每个ViewController在左上方都有一个按钮,当按下时按钮移动当前的ViewController,显示下面的菜单。

这很好但我想要的是删除此按钮并替换为NavigationController上的按钮(当前在紫色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];
}

1 个答案:

答案 0 :(得分:0)

你最好不要这样做......

  1. 这不是app delegate责任
  2. github / CocoaControls上有第三方实现提供此功能并管理导航栏
  3. 重做当前视图层次结构比强制从应用程序委托进行连接要好得多。

    • 应用代表的职责是响应应用级别事件(如前台/后台通知)。它可能涉及设置初始UI,但除此之外基本上什么也不应该。