在一个视图中创建两个操作表

发布于 2024-11-18 13:23:28 字数 1920 浏览 3 评论 0原文

我在一个视图中创建了两个操作表。有两个按钮,每个按钮都会启动一个操作表。

问题:当我按下两个操作表中的第一个选择时,会触发相同的操作。

这是我的代码:

-(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"];}
}

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"];}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2024-11-25 13:23:28

将每个操作表上的标记属性设置为不同的值。然后您可以检查 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
 }
}

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
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文