lucianf
lucianf

Reputation: 547

UIActionSheet firstOtherButtonIndex set incorrectly

I'm displaying an UIActionSheet on the iPad this way:

_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
                                           delegate:(id<UIActionSheetDelegate>)self
                                  cancelButtonTitle:nil
                             destructiveButtonTitle:nil
                                  otherButtonTitles:nil];

if (special_case) {
    _actionSheet.destructiveButtonIndex = [_actionSheet addButtonWithTitle:@"Discard Action"];
}

[_actionSheet addButtonWithTitle:@"Action One"];
[_actionSheet addButtonWithTitle:@"Action Two"];
_actionSheet.cancelButtonIndex = [_actionSheet addButtonWithTitle:@"Cancel"];

[_actionSheet showFromBarButtonItem:self.myActionButton animated:YES];

While both cancelButtonIndex and destructiveButtonIndex work fine, the weird thing is that in actionSheet:didDismissWithButtonIndex: I'm getting firstOtherButtonIndex set to -1, which is obviously wrong because my Action One button has an index of 1, while Action Two has an index of 2.

Obviously the whole point of the above is to only display the destructive button only in a special case (otherwise I would have passed all titles in the init call and probably things would have been sweet).

What am I missing?

Upvotes: 1

Views: 771

Answers (1)

Sulthan
Sulthan

Reputation: 130102

firstOtherButtonIndex is probably set only in the initWithTitle method. You have to set it manually if you are adding buttons manually. The problem can be also caused by the fact that you are adding the cancel button after adding the other buttons. Normally the indices of destructive and cancel buttons are lower than firstOtherButtonIndex.

What about doing it the easy way?


_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
                                           delegate:(id)self
                                  cancelButtonTitle:@"Cancel"
                             destructiveButtonTitle:(special_case ? @"Discard" : nil)
                                  otherButtonTitles:@"Action One", @"Action Two", nil];

Upvotes: 2

Related Questions