0

我是 iOS 开发的新手,我试图在点击 barButtomItem 时显示一个弹出框。到目前为止,我有这个:

-(IBAction)shareButtonPressed :(id)sender{    
    UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:@"ShareOptionsViewController" bundle:nil];
    popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);

    self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
    [self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny  animated:NO];
}

此代码有效,当我点击正确的按钮时,会显示弹出框。问题是当显示 popOver 时,如果我再次点击该按钮,应用程序就会崩溃。我知道是因为它试图分配一个已经创建的变量。我认为解决方案应该是在弹出框加载时禁用按钮,但我不知道如何在弹出框被解除后再次激活。

我对吗?popOver 关闭时如何启用按钮?

4

5 回答 5

1

首先,您应该禁用 shareButtonPressed 方法中的按钮。

-(IBAction)shareButtonPressed :(id)sender{    
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:@"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);

self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.myButton setEnabled:NO];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny  animated:NO];
}

然后你让你的视图控制器符合 UIPopoverControllerDelegate 协议。之后,您实现 popoverControllerDidDismissPopover 方法。

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
if (!self.myButton.enabled) // Just to make sure the button is disabled.
    [self.myButton setEnabled:YES];

}
于 2013-02-21T11:38:23.763 回答
0

只需检查实例是否已经创建。

-(IBAction)shareButtonPressed:(id)sender
{
    if (self.popoverController == nil) {
        self.popoverController = alloc/init...;
        [_popoverController present...];
    } else {
        if (self.popoverController.popoverVisible)
            [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController = nil;
    }
}

有可能会在动画完成之前被垃圾收集,从而产生无效的用户体验。然后,如果可能的话,我会在 viewDidLoad 或 contructor 等中预先创建实例,然后根据需要显示和关闭,检查可见属性。

于 2013-02-21T11:27:24.167 回答
0

看看这个“事件-->责任”列表:

1) 按下按钮 --> MainViewController - IBAction

2) 显示弹出框 --> MainViewController - IBAction

3) 按钮禁用 --> MainViewController - IBAction

4) Popover dissmis --> popoverDelegate - popoverControllerDidDismissPopover

5) 按钮启用 --> popoverDelegate - popoverControllerDidDismissPopover

因此,如果您的弹出框的委托是 MainViewController,您将可以访问该按钮,如果它是一个不同的类,您可能必须设置一个协议才能重新启用该按钮。

如果您需要任何进一步的解释,请告诉我...

于 2013-02-21T11:31:46.990 回答
0

您可以检测弹出框是否显示和关闭:

- (IBAction)shareButtonPressed :(id)sender{
    if (self.popoverController) {
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController = nil;
    } else {
        UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:@"ShareOptionsViewController" bundle:nil];
        popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);

        self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
        [self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny  animated:NO];
    }
}

或者如果您只想禁用该按钮:

- (IBAction)shareButtonPressed :(id)sender{
    ...
    UIButton *button = (UIButton *)sender;
    button.enabled = NO;
}

但在这种情况下,您必须检测用户何时关闭popOver以再次启用该按钮。您可以在课堂上采用UIPopoverController委托(参见文档)来做到这一点

于 2013-02-21T11:34:35.317 回答
0

只需像这样修改您的代码...

-(IBAction)shareButtonPressed :(id)sender{    
        UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:@"ShareOptionsViewController" bundle:nil];
        popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);

         if(self.popoverController)
          {
            [self.popovercontroller dismissPopoverAnimated:YES];
            self.popovercontroller = nil;
           }   
        self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
        [self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny  animated:NO];
    }
于 2013-02-21T11:35:15.733 回答