Creating two action sheets in one view
Asked Answered
E

1

5

I created two action sheets in one view. There are two buttons, each will initiate one action sheet.

The problem: when i press on first choice in both action sheets the same action is triggered.

Here's my code:

-(IBAction) ChangeArrow:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Change Arrow"
                                                         delegate:self
                                                cancelButtonTitle:@"cancel"
                                           destructiveButtonTitle:@"Red"
                                                otherButtonTitles:@"Blue",@"Black",nil];
[actionSheet showInView:self.view];
[actionSheet release];}
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex ==[actionSheet destructiveButtonIndex]) {
    self.bar.image=[UIImage imageNamed:@"red"];

}
else if(buttonIndex == 1){
    self.bar.image=[UIImage imageNamed:@"blue"];

}
else if(buttonIndex == 2){
    self.bar.image=[UIImage imageNamed:@"dark"];}
}

//Second Action sheet:

-(IBAction) Background:(id)sender{
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:@"Change Background"
                                                         delegate:self
                                                cancelButtonTitle:@"cancel"
                                           destructiveButtonTitle:@"Sky"
                                                otherButtonTitles:@"Thumbs",@"Smiley",nil];
[actionSheet2 showInView:self.view];
[actionSheet2 release];
} 
- (void) actionSheet2: (UIActionSheet *)actionSheet2 didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex ==[actionSheet2 destructiveButtonIndex]) {
    self.background.image=[UIImage imageNamed:@"sky"];

}
else if(buttonIndex == 1){
    self.background.image=[UIImage imageNamed:@"thumbs"];

}
else if(buttonIndex == 2){
    self.background.image=[UIImage imageNamed:@"smiley"];}
}
Euler answered 6/7, 2011 at 18:32 Comment(1)
daniel dickison thnx , i'm still having some problems when writing a code :PEuler
B
25

Set the tag property on each actionsheet to a different value. Then you can check sender.tag to see which action sheet called your method.

Ex.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Change Arrow"
                                                     delegate:self
                                            cancelButtonTitle:@"cancel"
                                       destructiveButtonTitle:@"Red"
                                            otherButtonTitles:@"Blue",@"Black",nil];
actionSheet.tag = 1;
UIActionSheet *actionSheet2 = [[UIActionSheet alloc] initWithTitle:@"Change Arrow"
                                                         delegate:self
                                                cancelButtonTitle:@"cancel"
                                           destructiveButtonTitle:@"Red"
                                                otherButtonTitles:@"Blue",@"Black",nil];
actionSheet2.tag = 2;

Then

- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
 if(actionSheet.tag == 1) {
     //do something
 } else if(actionSheet.tag == 2) {
     //do something else
 }
}
Bullate answered 6/7, 2011 at 18:34 Comment(2)
to add to David's correct answer, you check to see which sender it was in the delegate method.Nurseryman
+1 to David: see edited example in Davids answer for the best/easiest wayNurseryman

© 2022 - 2024 — McMap. All rights reserved.