UIActionSheet event not firing

2019-03-05 02:25发布

问题:

I have a UIActionSheet to provide user options on a table view controller. I have implemented this before without issue, but for some reason now, my event handler does not fire when a button is clicked. In my header file, I have implemented the proper delegate as follows:

@interface GaugeDetailTableViewController : UITableViewController <UIActionSheetDelegate>

In my implementation, I have the following code to support the action sheet:

-(void)loadPopup{
UIActionSheet *popup = [[UIActionSheet alloc]
                        initWithTitle:@"Options"
                        delegate:nil
                        cancelButtonTitle:@"Cancel"
                        destructiveButtonTitle:nil
                        otherButtonTitles:@"Add to Favorites", @"Gauge Detail Help", @"Main Menu", nil];
popup.actionSheetStyle = UIActionSheetStyleDefault;

[popup showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%d\n", buttonIndex);
}

The action sheet appears properly on the view controller, but for some reason, the action sheet click event handler is never reached. Any suggestions? Thanks!

回答1:

You need to set the delegate:

UIActionSheet *popup = [[UIActionSheet alloc]
                        initWithTitle:@"Options"
                        delegate:self
                        cancelButtonTitle:@"Cancel"
                        destructiveButtonTitle:nil
                        otherButtonTitles:@"Add to Favorites", @"Gauge Detail Help", @"Main Menu", nil];
popup.actionSheetStyle = UIActionSheetStyleDefault;


回答2:

It looks like you have forgotten to set the delegate. You need to implement UIActionSheetDelegate (which it looks like you have) and then set the delegate!