我在一个视图中创建了两个操作表。有两个按钮,每个按钮将启动一个操作表。
问题:当我按下两个操作表中的第一个选项时,会触发相同的操作。
这是我的代码:
-(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"];}
}
//第二个行动表:
-(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"];}
}
答案 0 :(得分:25)
将每个操作表上的tag属性设置为其他值。然后,您可以检查sender.tag以查看哪个操作表称为您的方法。
实施例
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;
然后
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(actionSheet.tag == 1) {
//do something
} else if(actionSheet.tag == 2) {
//do something else
}
}