Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating two action sheets in one view

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"];}
}
like image 286
Dani Avatar asked Jul 06 '11 18:07

Dani


1 Answers

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
 }
}
like image 96
Dave Kasper Avatar answered Oct 18 '22 09:10

Dave Kasper