【问题标题】:Executing ViewController's methods from UINavigationController从 UINavigationController 执行 ViewController 的方法
【发布时间】:2014-01-17 12:34:47
【问题描述】:

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

截图说明:

目前,每个 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];
}

【问题讨论】:

    标签: objective-c cocoa-touch ios7 uinavigationcontroller


    【解决方案1】:

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

    1. 这不是应用委托的责任
    2. 在 github / CocoaControls 上有 3rd 方实现提供此功能并管理导航栏

    重新设计当前的视图层次结构比从应用代理强制连接要好得多。

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

    【讨论】:

    • 我明白你的意思,但这似乎是一件很容易实现的事情——一个按钮,它获取当前的 ViewController 并运行它的一个方法。我想我错过了一些相当基本的东西。
    • 并非如此,使用像github.com/ECSlidingViewController/ECSlidingViewController 这样的第三方控制器将节省您的时间并带来更好的应用程序设计。你不应该在每个视图控制器中都有滑动功能......
    • 我实际上正在使用那个第 3 方控制器 (ECSlidingViewController)。它附带的教程实际上展示了每个 VC 中使用的滑块功能。我添加了 NavigationController 作为整个应用程序中的静态菜单。
    • 所以按钮应该连接到 ECSlidingViewController,而不是应用代理。
    • 如果我将按钮留在每个 ViewController 上,而不是每个 VC 上的一个按钮,我想要 UINavigationControl 上的一个按钮,按钮工作正常(根据教程)。由于 UIC 是内置在 AppDelegate 中的,因此在此处添加此按钮似乎是合乎逻辑的。
    猜你喜欢
    • 2012-12-29
    • 2013-01-02
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多