修改呈现模式视图的视图控制器的 navigationItem?
我在基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.