强制隐藏细节视图后未调用UISplitViewController willHideViewController/willShowViewController [英] UISplitViewController willHideViewController/willShowViewController not called after forcing detail view to be hidden

查看:53
本文介绍了强制隐藏细节视图后未调用UISplitViewController willHideViewController/willShowViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPad App,它使用UISplitViewController作为根控制器,已经设置了委托,并且对DetailView使用UINavigationController,它似乎正常工作.

I have an iPad App which uses a UISplitViewController as the root controller, I have set up the delegate and I am using a UINavigationController for the DetailView and it seems to work normally.

当在DetailView部分中显示特定的视图控制器时,我希望MasterViewContoller始终处于隐藏状态(所有旋转).我可以使用UISplitViewController委托上的shouldHideViewController方法来做到这一点.

I want the MasterViewContoller to always be hidden (on all rotations) when a particular viewcontroller is displayed in the DetailView Section. I can do this using the shouldHideViewController method on the UISplitViewController Delegate.

但是,如果我这样做,则当我将DetailView部分中的ViewController更改为另一个时,将显示MasterView,但在委托中均不会调用willHideViewController/willShowViewController.

However if I do, then when I change the ViewController in the DetailView Section to another the MasterView is displayed but neither willHideViewController/willShowViewController is called in the delegate.

如果我旋转设备并向后旋转,它们会被调用,并且可以按预期工作,但是直到设备旋转并旋转回原始方向后,它们才会被调用.

If I rotate the device and rotate back they are called and it works as expected, but they are not called until the device has been rotated and rotated back to the original orientation.

这将导致在显示MasterView之后的第一个旋转中不显示弹出按钮.

This causes the popover button to not be displayed on the first rotation after the MasterView has been displayed.

- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {

// Keep references to the popover controller and the popover button, and tell the detail view controller to show the button.
barButtonItem.title = @"Survey Sections";
self.popoverController = pc;
self.rootPopoverButtonItem = barButtonItem;

UIViewController <SubstitutableDetailViewController> *detailViewController = [self.splitViewController.viewControllers objectAtIndex:1];

if ([detailViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController *detailNavController = (UINavigationController *)detailViewController;
    [[detailNavController.viewControllers objectAtIndex:0] showRootPopoverButtonItem:rootPopoverButtonItem];
}

}

- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

// Nil out references to the popover controller and the popover button, and tell the detail view controller to hide the button.
UIViewController <SubstitutableDetailViewController> *detailViewController = [self.splitViewController.viewControllers objectAtIndex:1];

if ([detailViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController *detailNavController = (UINavigationController *)detailViewController;
    [[detailNavController.viewControllers objectAtIndex:0] invalidateRootPopoverButtonItem:rootPopoverButtonItem];
}

self.popoverController = nil;
self.rootPopoverButtonItem = nil;

}

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

UIViewController *detailViewController = [svc.viewControllers objectAtIndex:1];

if ([detailViewController isMemberOfClass:[MySurveysViewController class]]) {
    return YES;
}else {

    if (UIInterfaceOrientationIsLandscape(orientation)) {
        return NO;
    }else {
        return YES;
    }

}

}

推荐答案

我遇到了一个非常相似的问题,将Apple的示例代码与SubstitutableDetailViewController委托结合了旋转使用.

I was having a very similar problem, using Apple's sample code with the SubstitutableDetailViewController delegate combined with rotation.

我所做的是在方向为横向时存储了BOOL,并使用以下代码在willRotateToInterfaceOrientation上对其进行了更新:

What I did was store a BOOL for when the orientation was landscape, updating it on willRotateToInterfaceOrientation with this code:

isLandscape = UIInterfaceOrientationIsLandscape(self.interfaceOrientation);

viewWillAppear上,我检查了当前方向是否为横向,如果它与存储的值不匹配,则调用此方法:

On viewWillAppear I checked whether the current orientation is landscape, and if it did not match the stored value, called this method:

-(void)adjustLayoutForOrientation{
    if (isLandscape) {
        [self invalidateRootPopoverButtonItem:  self.navigationController.navigationItem.leftBarButtonItem];
}else{
        LeftViewController *lvc = (LeftViewController *)[self.splitViewController delegate];
        [self showRootPopoverButtonItem:  lvc.rootPopoverButtonItem ];
    }
}

很明显,我使用的是Apple提供的委托代码,但是为了完整起见,这些方法是可以轻松实现的:

Obviously I am using the delegation code provided by Apple, but for completeness those methods are these and can be implemented easily:

#pragma mark -
#pragma mark Managing the popover

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Add the popover button to the left navigation item.
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
}

主视图控制器(左侧)正在维护对rootPopoverButtonItem的引用,从而使这项工作得以实现.

The master view controller (left side) is maintaining the reference to the rootPopoverButtonItem, which makes this work.

编辑:请注意,我也在viewDidLoadwillRotateToInterfaceOrientation上调用我的adjustLayoutForOrientation方法.一种更好的方法可能是在通知中心注册,但我没有确定背景/不可见视图控制器是否会收到这些通知...

EDIT: Note that I am also calling my adjustLayoutForOrientation method on viewDidLoad and willRotateToInterfaceOrientation... A better way might be to register with the Notification Center but I wasn't sure if background/invisible view controllers would get these notifications...

这篇关于强制隐藏细节视图后未调用UISplitViewController willHideViewController/willShowViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆