iPad UIActionSheet点击外面解雇 - UIActionSheet的状态?

时间:2012-03-08 09:23:58

标签: ipad uiactionsheet sharekit

我正在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值。我怎样才能测试这种解雇?

1 个答案:

答案 0 :(得分:2)

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

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

修改

为了确保收到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