Skip to content

Instantly share code, notes, and snippets.

@blixt
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save blixt/edfe1cb86ecb0e34f63c to your computer and use it in GitHub Desktop.

Select an option

Save blixt/edfe1cb86ecb0e34f63c to your computer and use it in GitHub Desktop.
// Observed bug(?): When adding a button after creating UIAlertView, the firstOtherButtonIndex value
// is stuck as -1 instead of the correct value, 1, even though the documentation states that the two
// invocations below should be equivalent.
NSString *closeText = @"Cancel";
NSString *actionText = @"OK";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:closeText
otherButtonTitles:nil];
if (actionText) {
[alertView addButtonWithTitle:actionText];
}
NSLog(@"alertView cancel:%ld firstOther:%ld", (long)alertView.cancelButtonIndex, (long)alertView.firstOtherButtonIndex);
// alertView cancel:0 firstOther:-1
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:closeText
otherButtonTitles:actionText, nil];
NSLog(@"alertView cancel:%ld firstOther:%ld", (long)alertView.cancelButtonIndex, (long)alertView.firstOtherButtonIndex);
// alertView cancel:0 firstOther:1

The Apple documentation on otherButtonTitles has this to say:

The title of another button.
Using this argument is equivalent to invoking addButtonWithTitle: with this title to add more buttons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment