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

时间:2011-06-26 02:15:48

标签: iphone ios ipad uinavigationcontroller uisplitviewcontroller

我在基于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];
    }
}

1 个答案:

答案 0 :(得分:1)

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