Reputation: 1471
Every time I press a cancel button in UIActionSheet, it runs a method. I have no idea why, I checked whole my code many times, but I still can't see the problem. Could you help me to find it ?
-(IBAction)moreOptions
{
giftTitle = self.title;
if(![giftTitle isEqualToString:@"bla"])
{
actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
delegate:self
cancelButtonTitle:@"Back"
destructiveButtonTitle:nil
otherButtonTitles:@"Send via email",
@"Read in Wikipedia"
, nil];
}
else
{
actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
delegate:self
cancelButtonTitle:@"Back"
destructiveButtonTitle:nil
otherButtonTitles:@"Send via email",
@"Read in Wikipedia", @"Pineapple mode"
, nil];
}
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view.window];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// выстраеваем дальнейшие действия кнопок
switch (buttonIndex)
{
case 0:
[self showPicker];
break;
case 1:
[self goWiki];
break;
case 2:
[self showPineapple];
break;
default:
break;
}
}
So it runs method showPineapple
. Please help !
Upvotes: 0
Views: 1797
Reputation: 2533
You need to implement something like this:
Change your if
and else
sections to add a unique tag for each UIActionSheet:
if(![giftTitle isEqualToString:@"bla"]) {
actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
delegate:self
cancelButtonTitle:@"Back"
destructiveButtonTitle:nil
otherButtonTitles:@"Send via email", @"Read in Wikipedia" , nil];
actionSheet.tag = 10;
} else {
actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
delegate:self
cancelButtonTitle:@"Back"
destructiveButtonTitle:nil
otherButtonTitles:@"Send via email", @"Read in Wikipedia", @"Pineapple mode", nil];
actionSheet.tag = 20;
}
Then look for the tag in the actionSheet:clickedButtonAtIndex: message handler:
case 2:
if (actionSheet.tag == 20)
[self showPineapple];
break;
This means [self showPineapple]
will only run in the else
scenario, while nothing will happen in the if
scenario (just as nothing will happen for buttonIndex
3 in the else
scenario (where the Cancel button is indeed at index 3) .
Upvotes: 0
Reputation: 31081
Ya when you press cancel button on action sheet then it's delegate function always call, with the last index.
If you are implementing multiple actionsheet then just use it by tag value.
Upvotes: 1