2

I am using the ShareKit SHKActionSheet (based on UIActionSheet) for an app on the iPad:

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

I want to perform some action depending on whether actionSheet is in view or not. The problem is that when a user taps outside actionSheet to dismiss it from view (default behaviour on iPad) actionSheet no longer points to a valid SHKActionSheet nor does it have a nil value. How can I test for such a dismiss?

camden_kid
  • 12,591
  • 11
  • 52
  • 88

1 Answers1

2

The UIActionSheet class has a visible property. This returns YES if the action sheet is showing, NO if it isn't. That should enable you to know whether or not it's been dismissed.

You could also implement some UIActionSheetDelegate methods to know when it's been dismissed or cancelled using actionSheetCancel: and/or actionSheet:didDismissWithButtonIndex:.

EDIT:

To be sure to receive the delegate calls of your UIActionSheet, be sure to indicate in your controller's interface declaration (.h):

@interface YourViewController : UIViewController<UIActionSheetDelegate>

@end

Then in the controller's implementation (.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");
}

EDIT 2:

I've just had a look at the code of the SHKActionSheet class and it turns out it doesn't implement the actionSheetCancel: method, which is why you're not receiving it. It does, however, implement the actionSheet:didDismissWithButtonIndex: method :

- (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];
} 

If you want to be notified of the actionSheetCancel: method, simply add this to the SHKActionSheet.m file :

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {

    [super actionSheetCancel:actionSheet];
}

The delegate method in your controller will then be called properly :)

UIActionSheet Class Reference

UIActionSheetDelegate Protocol Reference

Mutix
  • 4,196
  • 1
  • 27
  • 39
  • The problem is that the app crashes when I test actionSheet.visible after it has been dismissed. – camden_kid Mar 08 '12 at 14:26
  • Ok, in that case try implementing the delegate methods I mentioned. Make sure you set the action sheet's delegate property: `actionSheet.delegate = self;` – Mutix Mar 08 '12 at 14:30
  • In my setup I do the following: SHKActionSheet *as = [[SHKActionSheet alloc] initWithTitle:SHKLocalizedString(@"Share") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; but the delegate line is giving me a warning that self is not of type UIActionSheetDelegate. I think this is stopping me from setting up delegates. – camden_kid Mar 08 '12 at 14:55
  • Ok I've edited my answer to show how to use the delegate methods – Mutix Mar 08 '12 at 15:01
  • Thanks for all your help. I didn't get those delegates to work. However, it lead me to put an NSlog in dismissWithClickedButtonIndex which in fact does get called after a dimiss (the "ClickedButton" part was misleading). – camden_kid Mar 08 '12 at 15:12
  • You're welcome. I edited again in case you'd rather use the `actionSheetCancel:` method instead of the `dismissWithClickedButtonIndex:` method – Mutix Mar 08 '12 at 15:24