知道拆分视图控制器的主控器何时被手势解除

时间:2014-03-28 01:32:38

标签: ios objective-c ipad uisplitviewcontroller master-detail

我在iOS 7上实现主细节应用程序,主视图控制器是一个导航控制器,可以浏览两个表视图控制器。细节是一个显示图像的简单视图控制器。

我在细节中实现了UISplitViewController个方法:

- (BOOL) splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return UIInterfaceOrientationIsPortrait(orientation);
}

- (void) splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    UINavigationController* nav= (UINavigationController*)aViewController;
    barButtonItem.title= nav.topViewController.title;
    self.navigationItem.leftBarButtonItem= barButtonItem;
}

- (void) splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    self.navigationItem.leftBarButtonItem= nil;
}

问题是当用户用手势取消主视图控制器时,不会调用splitViewController:willHideViewController:withBarButtonItem:forPopoverController:

这会出现如下问题:如果用户通过主导航控制器导航然后关闭主控制器,则标签栏按钮项目会显示旧标题。

为了更清楚,我将解释一步一步发生的事情。主人的导航控制器的根导航控制器的标题是" Top Places"。推送的视图控制器的标题是"照片"。当应用程序启动时,在纵向模式下没有主人:

enter image description here

用户出示"热门地点"查看控制器:

enter image description here

然后用户导航到第二个视图控制器:"照片":

enter image description here

然后用户解雇了主人,但是标签栏按钮项仍然保留了它的旧标题"热门地点":

enter image description here

问题是拆分视图控制器委托没有通过手势解除主控制器时调用的方法。

2 个答案:

答案 0 :(得分:3)

根本没有UISplitViewControllerDelegate委托方法来检测弹出窗口在纵向方向时被解除的时间。它如何被解雇并不重要。如果您想知道,请为弹出控制器设置一个委托。

从横向旋转到纵向时,将调用splitViewController:willHideViewController:withBarButtonItem:forPopoverController:委托方法。

答案 1 :(得分:1)

您不需要知道主视图何时被解除,只需根据导航控制器当前topViewController的导航项标题设置按钮的标题。您可以通过使您的详细控制器成为导航控制器的代表

来完成此操作
@interface DetailController ()
@property (strong,nonatomic) NSString *barButtonTitle;
@end

@implementation DetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [(UINavigationController *)self.splitViewController.viewControllers[0] setDelegate:self];
    self.splitViewController.delegate = self;
    self.navigationItem.leftBarButtonItem.title = [[(UINavigationController *)self.splitViewController.viewControllers[0] topViewController] navigationItem].title;
}


- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.barButtonTitle = viewController.navigationItem.title;
    self.navigationItem.leftBarButtonItem.title = self.barButtonTitle;
}



- (BOOL) splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
    return UIInterfaceOrientationIsPortrait(orientation);
}



- (void) splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
    barButtonItem.title= self.barButtonTitle;
    self.navigationItem.leftBarButtonItem= barButtonItem;
}



- (void) splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    self.navigationItem.leftBarButtonItem= nil;
}