2

我正在编写一个 iPad 应用程序,我的一个屏幕上有很多小按钮,按下时会在源自该按钮的弹出窗口中显示一个文本句子。目前所有弹出框都是使用情节提要创建的,我将弹出框控制器存储在我UIViewController的如下:

@property (nonatomic, strong) UIPopoverController *myPopoverController;

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]]) 
    {
        UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
        self.myPopoverController = popoverSegue.popoverController;
    }
}

但是,我想不出一个处理旋转的好方法。对,我的didRotate方法如下所示:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (self.myPopoverController) 
    {
        [self.myPopoverController dismissPopoverAnimated: NO];
        [self.myPopoverController presentPopoverFromRect:??????  inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    }
}

但是,我不知道从哪里显示弹出框,因为它们可能来自我屏幕上的任何小按钮。有什么建议么?请记住,这些是非常简单的弹出框,因此一大堆新代码并不理想。

4

1 回答 1

1

您最好的选择可能是在主视图控制器中创建另一个属性,以保持对按下按钮的引用。就像是:

@property (nonatomic, strong) UIPopoverController *myPopoverController;
@property (nonatomic, weak) UIView *popoverButton;

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue isKindOfClass:[UIStoryboardPopoverSegue class]]) 
    {
        UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
        self.myPopoverController = popoverSegue.popoverController;

        //The sender in prepareForSegue should be the view used to initiate the segue.
        popoverButton = (UIView *)sender;
    }
}

完成后,您可以修改轮换代码:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (self.myPopoverController) 
    {
        [self.myPopoverController dismissPopoverAnimated: NO];
        [self.myPopoverController presentPopoverFromRect:popoverButton.frame  inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    }
}

保持对按下按钮的引用不会占用存储指针的更多资源,并且保持引用弱应该避免保留周期(毕竟,您的视图控制器不拥有按钮,按钮的超级视图拥有它)。

于 2012-08-07T15:06:29.887 回答