【问题标题】:iPad UIActionSheet tap outside to dismiss - state of UIActionSheet?iPad UIActionSheet 点击外部以关闭 - UIActionSheet 的状态?
【发布时间】:2012-03-08 09:23:57
【问题描述】:

我正在为 iPad 上的应用程序使用 ShareKit SHKActionSheet(基于 UIActionSheet):

SHKItem *item = [SHKItem URL:url title:@""];
actionSheet = [SHKActionSheet actionSheetForItem:item];
[actionSheet showFromToolbar:self.navigationController.toolbar];

我想根据 actionSheet 是否在视图中执行一些操作。问题在于,当用户在 actionSheet 外部点击以将其从视图中关闭(iPad 上的默认行为)时,actionSheet 不再指向有效的 SHKActionSheet,也没有 nil 值。我如何测试这种解雇?

【问题讨论】:

    标签: ipad uiactionsheet sharekit


    【解决方案1】:

    UIActionSheet 类有一个visible 属性。如果操作表正在显示,则返回YES,否则返回NO。这应该能让你知道它是否被解雇了。

    您还可以使用actionSheetCancel: 和/或actionSheet:didDismissWithButtonIndex: 实现一些UIActionSheetDelegate 方法来了解它何时被解除或取消。

    编辑:

    为确保接收 UIActionSheet 的委托调用,请务必在控制器的接口声明 (.h) 中指明:

    @interface YourViewController : UIViewController<UIActionSheetDelegate>
    
    @end
    

    然后在控制器的实现中(.m):

    - (void)actionSheetCancel:(UIActionSheet *)actionSheet {
    
        NSLog(@"action sheet is about to be cancelled");
    }
    
    - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
        NSLog(@"action sheet was dismissed");
    }
    

    编辑 2:

    我刚刚查看了SHKActionSheet 类的代码,结果发现它没有实现actionSheetCancel: 方法,这就是您没有收到它的原因。但是,它确实实现了actionSheet:didDismissWithButtonIndex: 方法:

    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
    {
        // Sharers
        if (buttonIndex >= 0 && buttonIndex < sharers.count)
        {
            [NSClassFromString([sharers objectAtIndex:buttonIndex]) performSelector:@selector(shareItem:) withObject:item];
        }
    
        // More
        else if (buttonIndex == sharers.count)
        {
            SHKShareMenu *shareMenu = [[SHKCustomShareMenu alloc] initWithStyle:UITableViewStyleGrouped];
            shareMenu.item = item;
            [[SHK currentHelper] showViewController:shareMenu];
            [shareMenu release];
        }
    
        [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
    } 
    

    如果您想收到actionSheetCancel: 方法的通知,只需将其添加到 SHKActionSheet.m 文件中即可:

    - (void)actionSheetCancel:(UIActionSheet *)actionSheet {
    
        [super actionSheetCancel:actionSheet];
    }
    

    然后您的控制器中的委托方法将被正确调用:)

    UIActionSheet Class Reference

    UIActionSheetDelegate Protocol Reference

    【讨论】:

    • 问题是当我测试actionSheet.visible时应用程序崩溃了。
    • 好的,在这种情况下尝试实现我提到的委托方法。确保设置了操作表的委托属性:actionSheet.delegate = self;
    • 在我的设置中,我执行以下操作: SHKActionSheet *as = [[SHKActionSheet alloc] initWithTitle:SHKLocalizedString(@"Share") delegate:self cancelButtonTitle:nil responsiveButtonTitle:nil otherButtonTitles:nil];但是委托行给了我一个警告,即 self 不是 UIActionSheetDelegate 类型。我认为这会阻止我设置代表。
    • 好的,我已经编辑了我的答案以展示如何使用委托方法
    • 感谢您的所有帮助。我没有让那些代表工作。但是,它导致我在dismissWithClickedButtonIndex中放置了一个NSlog,实际上它确实在关闭后被调用(“ClickedButton”部分具有误导性)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多