以编程方式显示和隐藏UISplitViewController中的popover(MasterViewController)(Master-Detail Template)

时间:2013-06-24 07:30:59

标签: ios6 uisplitviewcontroller show-hide popover master-detail

我想添加一个显示/隐藏弹出窗口的按钮,类似于DropBox应用程序。 (在风景和肖像中)

DropBoxAppImageLink

我尝试了许多解决方案,但在这个阶段我甚至不想用我的尝试来浑水。如果您已经完成了这项工作,或者知道如何做到这一点,请向我发送正确的方向!

谢谢!

2 个答案:

答案 0 :(得分:2)

看起来很简单。

将某个对象设置为splitViewController的委托。在我的情况下(我以编程方式创建所有viewcontrollers),这是appdelegate。

UISplitViewController* splitViewController = [[UISplitViewController alloc] init];
        [splitViewController setViewControllers:@[navigationViewController1, navigationViewController2]];
        splitViewController.delegate = self;

实施委托方法以纵向隐藏主人:

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

实际上添加barButtonItem:

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

            UINavigationController* slaveNavigationViewController = svc.viewControllers[1];
            UIViewController*       slaveViewController = slaveNavigationViewController.viewControllers[0];
            [barButtonItem setTitle:@"Your master title"];
            slaveViewController.navigationItem.leftBarButtonItem = barButtonItem;
        }

在此方法中,您将获得自定义并添加到slaveViewController的barButtonItem。

最后一个,横向移除按钮:

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    UINavigationController* slaveNavigationViewController = svc.viewControllers[1];
    UIViewController*       slaveViewController = slaveNavigationViewController.viewControllers[0];
    [barButtonItem setTitle:@"Drops"];
    slaveViewController.navigationItem.leftBarButtonItem = nil;
}

就是这样。

答案 1 :(得分:2)

有一种更简单,没有文档记录的方法。对于现有的UIButton:

[button addTarget: theSplitViewController action: @selector(toggleMasterVisible:) forControlEvents:UIControlEventTouchUpInside];

此目标/操作与willHideViewController函数中发送的barButtonItem相同。