2

可以通过编程方式创建一个 UIPopoverController,然后设置它的 viewController、passthrough 视图等:

- (IBAction)showCustomPopoverButtonHandler:(id)sender {
    if (self.colorPicker == nil) {
        self.colorPicker = [[ColorPickerTableViewController alloc] initWithStyle:UITableViewStylePlain];
        _colorPicker.delegate = self;
        self.colorPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_colorPicker];
    }


    [self.colorPickerPopover setPassthroughViews:@[self.containerView]];
    [self.colorPickerPopover presentPopoverFromRect:self.showCustomPopoverButton.frame
                                             inView:self.view
                           permittedArrowDirections:UIPopoverArrowDirectionRight
                                           animated:YES];
}

但是,我想利用故事板/序列。采用这种方法时,我看不到如何获取对 UIPopoverController 实例的编程引用:

在此处输入图像描述

有人知道怎么做吗?我希望像

UIPopoverController* pvc = self.popoverController;

我还列举了 childViewControllers 来检查他们的类类型:

   // Get references to child view controllers.
    for(NSUInteger index = 0; index < self.childViewControllers.count; index++){
        id obj = self.childViewControllers[index];
        if([obj isKindOfClass:[UIPopoverController class]]){
            self.menuPopoverController = self.childViewControllers[index];
        }
    }
4

1 回答 1

4

在苹果文档中找到答案:UIStoryboardPopoverSegue Class Ref

我在 prepareForSegue 中投射了 segue,并将 strong iVar 设置为 popoverController 属性,如下所示:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:YOUR_SEGUE_IDENTIFIER]){
        // Typical delegate setup.....
        UIStoryboardPopoverSegue* ps = (UIStoryboardPopoverSegue*)segue;
        self.popoverController = ps.popoverController;
        [self.popoverController setPassthroughViews:/* array of views */];
    }
}

奇迹般有效。

或者,您可以像这样使用它(以相同的方法)

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue isKindOfClass:[UIStoryboardPopoverSegue class]]){
        self.menuPopoverController = ((UIStoryboardPopoverSegue*)segue).popoverController;
    }
}
于 2013-03-04T18:35:28.240 回答