我正在编写一个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];
}
}
然而,我不知道在哪里提供弹出窗口,因为它们可能源自我屏幕上的任何小按钮。有什么建议?请记住,这些是非常简单的弹出窗口,因此一大堆新代码并不理想。
答案 0 :(得分: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];
}
}
保持对按下的按钮的引用不再占用存储指针的资源,并且保持引用弱应该避免保留周期(毕竟,你的视图控制器没有按钮,按钮的superview拥有它)。 / p>