I have discovered a strange problem when using UIActionSheet on the iPhone (iOS 4.2). Consider this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"TestSheet"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: nil];
[actionSheet addButtonWithTitle:@"one"];
[actionSheet addButtonWithTitle:@"two"];
[actionSheet addButtonWithTitle:@"three"];
[actionSheet addButtonWithTitle:@"four"];
[actionSheet addButtonWithTitle:@"five"];
[actionSheet addButtonWithTitle:@"six"];
//uncomment next line to see the problem in action
//[actionSheet addButtonWithTitle:@"seven"];
[actionSheet showInView:window];
[actionSheet release];
return YES;
}
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"buttonIndex: %d, cancelButtonIndex: %d, firstOtherButtonIndex: %d",
buttonIndex,
actionSheet.cancelButtonIndex,
actionSheet.firstOtherButtonIndex);
}
If you start this application, the actionsheet behaves as expected. That means the cancelButtonIndex is always 0, and the button indexes are reported correctly. 1 for button "one" and so on. If you comment in the line for adding the seventh button, the actionsheet produces a sort of tableview, with the cancel button on an extra line. If I press the "one" button in this case, the buttonindex variable is 0, but so is the cancelButtonIndex. It is impossible to tell if the user has tapped the "cancel" or the "one" button. That doesn't seem like it should be this way. Does anyone disagree? Thanks for your help.