如何在使用splitViewController时显示barButtonItem:willChangeToDisplayMode:在iOS8中

时间:2015-04-16 08:08:24

标签: ios objective-c ios8 uisplitviewcontroller

My App的结构喜欢这个:

UISplitViewController:

the master:NavigationController1->UITableViewController

the detail:NavigationController2->UIWebViewController

我希望在iPad上使用纵向模式时显示barButtonItem 我知道如何通过willHideViewController在iOS7中实现它:

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

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

但是,此方法在iOS 8中已弃用,我尝试使用:

-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{
    if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
        self.navigationItem.leftBarButtonItem = svc.displayModeButtonItem;
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
}

此方法仅在显示模式更改时有效,但在应用程序首次以纵向方向启动时才有效。 那么如何在第一次以纵向方向加载应用时显示barButtonItem。

2 个答案:

答案 0 :(得分:1)

您可以在视图控制器显示时添加条形按钮:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden)
    {
        UIBarButtonItem *barButtonItem = self.splitViewController.displayModeButtonItem;
        barButtonItem.title = @"Show master";
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }
}

这只会在主人当前被隐藏时添加按钮。

答案 1 :(得分:0)

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden){
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Course" style:self.splitViewController.displayModeButtonItem.style target:self.splitViewController.displayModeButtonItem.target action:self.splitViewController.displayModeButtonItem.action];
    }
}

-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{
    if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Course" style:svc.displayModeButtonItem.style target:svc.displayModeButtonItem.target action:svc.displayModeButtonItem.action];
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
}