我有一个使用UISplitViewController
. 当应用程序处于纵向模式时,我正在添加它的委托并在导航栏上分配一个按钮,以便能够在弹出视图中显示主视图控制器:
/*
@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
*/
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// do smth
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
#pragma mark - UISplitViewDelegate methods
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"", @"");
UIImage *imag = [[UIImage imageNamed:@"gray_button"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 7, 0, 7)];
UIImage *imag_pressed = [[UIImage imageNamed:@"gray_button_pressed"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 7, 0, 7)];
UIImage *more = [UIImage imageNamed:@"shows_button_pressed.png"];
[barButtonItem setBackgroundImage:imag forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[barButtonItem setBackgroundImage:imag_pressed forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[barButtonItem setImage:more];
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
如果在主视图控制器中选择了一个选项,则 RootViewController(基本上是我的拆分视图控制器的细节拆分)将更改为替换类型的 segue。我再次分配UISplitViewController
代表。我的问题是,除非我将设备置于横向模式然后返回纵向,否则在更改根目录后不会显示条形按钮。如果我这样做了,那么按钮就在那里并且可以正常工作。
如何在不将设备置于横向然后返回纵向的情况下使按钮出现?
PS:我已经尝试prepareForSegue:
在旧的根视图控制器中分配新的委托viewWillAppear:
方法并在新的根视图控制器中分配新的委托方法。同样的事情也会发生。另一件事:新的根视图控制器实现了<UISplitViewControllerDelegate>
更新 :
我发现该方法 :- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
仅在我从横向变为纵向后才被调用,这就是为什么我需要旋转设备以显示按钮的原因。
这两个委托之间的区别在于,第一个添加在 myAppDelegate.m
中,第二个添加在prepareForSegue:
方法中,在旧的根视图控制器中。只要应用程序启动,就会为旧的根视图控制器调用上述方法。