修改呈现模式视图的视图控制器的 navigationItem?

发布于 2024-11-17 10:58:11 字数 2112 浏览 4 评论 0原文

我在基于 UISplitViewController 的应用程序中展示了一个模式视图控制器。我已将默认详细视图替换为 UINavigationController。

在我的 UINavigationController 中,我实现了一些方法来在左上角显示分割视图控制器的“主”按钮。问题是,当设备方向发生变化而模态视图可见时,该按钮不会从我的导航控制器的主视图中消失。

什么可能导致此问题?

编辑:

我已将按钮的模板逻辑移至 UINavigationController 的子类中。问题是,当visibleViewController呈现一个modalViewcontroller时,就变成了visibleViewController。因此,由于某种原因,我的代码无法正确删除该按钮。这是我的代码:

代码:

#pragma mark - Split view support

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc{
    barButtonItem.title = NSLocalizedString(@"Menu", @"");

    //
    //  TODO: Handle cases where there is
    //  a modal view controller that is 
    //  being shown to the user.
    //


    [((UIViewController *)[self.viewControllers objectAtIndex:0]).navigationItem  setLeftBarButtonItem:barButtonItem];

    self.popoverController = pc;

}

// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{

    [self.visibleViewController.navigationItem setLeftBarButtonItem:nil];
    [self.popoverController dismissPopoverAnimated:YES];
    self.popoverController = nil;
}

//
//  Preserve navigation items across detail 
//  view loads in portrait mode.
//

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated{

    UIBarButtonItem *barItem = nil;

    if (self.visibleViewController.navigationItem.leftBarButtonItem != nil) {

        barItem = self.visibleViewController.navigationItem.leftBarButtonItem;
    }

    [super setViewControllers:viewControllers animated:animated];

    if (barItem != nil) {
        [self.visibleViewController.navigationItem setLeftBarButtonItem:barItem];
    }
}

I'm presenting a modal view controller in a UISplitViewController based app. I've swapped the default detail view for a UINavigationController.

In my UINavigationController, I've implemented some methods to show the "Master" button for the split view controller in the top left. The problem is that when the device orientation changes while the modal view is visible, the button does not disappear from the main view in my navigation controller.

What could be causing this issue?

EDIT:

I've moved the template logic for the button into a subclass of UINavigationController. The problem is that when the visibleViewController presents a modalViewcontroller, it becomes the visibleViewController. So, my code won't correctly remove the button for some reason. Here's my code:

Code:

#pragma mark - Split view support

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc{
    barButtonItem.title = NSLocalizedString(@"Menu", @"");

    //
    //  TODO: Handle cases where there is
    //  a modal view controller that is 
    //  being shown to the user.
    //


    [((UIViewController *)[self.viewControllers objectAtIndex:0]).navigationItem  setLeftBarButtonItem:barButtonItem];

    self.popoverController = pc;

}

// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{

    [self.visibleViewController.navigationItem setLeftBarButtonItem:nil];
    [self.popoverController dismissPopoverAnimated:YES];
    self.popoverController = nil;
}

//
//  Preserve navigation items across detail 
//  view loads in portrait mode.
//

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated{

    UIBarButtonItem *barItem = nil;

    if (self.visibleViewController.navigationItem.leftBarButtonItem != nil) {

        barItem = self.visibleViewController.navigationItem.leftBarButtonItem;
    }

    [super setViewControllers:viewControllers animated:animated];

    if (barItem != nil) {
        [self.visibleViewController.navigationItem setLeftBarButtonItem:barItem];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我认为不可能使用 SplitViewController 并在 navigationItem 中显示主视图的按钮。 UISplitViewControllerDelegate 专门设计用于与 UIBarButtonItem 一起使用,您可能必须实现自定义弹出控制器才能使其工作。

I don't think it's possible to use SplitViewController and display the button for the master view in a navigationItem. The UISplitViewControllerDelegate is specifically designed to work with a UIBarButtonItem, you'll probably have to implement a custom popover controller to get that to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文