1

嗨,我正在创建一个带有 2 个按钮的 UIActionSheet。现在我想要两个每个按钮做单独的工作。我如何在以下位置声明我的 2 个按钮:

- (void)actionSheet:(UIActionSheet *)menu
                didDismissWithButtonIndex:(NSInteger)buttonIndex 

我使用此代码:

if (buttonIndex != [menu cancelButtonIndex])    {
    // do somthing
}

但这意味着如果用户单击除 CANCEL BUTTON 以外的任何按钮都会做某事。谢谢你 。

4

3 回答 3

4

这将更通用。您可以将其扩展到任意数量的按钮:

- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex {

    switch (buttonIndex) {
        case 0:
            //do something
            break;
        case 1:
            //do something else
            break;
        default:
            break;
    }
}
于 2010-01-30T20:12:02.867 回答
1
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [menu cancelButtonIndex]) {
        // do something because the user clicked "cancel".
    } else {
        // do something because the user clicked "the other button".
    }
}
于 2010-01-30T19:50:19.520 回答
0
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex {

    switch (buttonIndex) {

        case 0:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" 
message:@"hooo" 
delegate:self
cancelButtonTitle:@"boo"
otherButtonTitles:@"yoo"];

            [alert show];
            [alert release];

            break;
            case 1:
            self.view.backgroundColor = [UIColor redColor];
                break;

        default:
            break;
    }
}
于 2010-01-30T22:12:21.640 回答