使用带有多个警报视图的buttonIndex

时间:2013-06-30 02:54:56

标签: ios uialertview

我出现时有大约4个不同标准的警报视图。在所有4个视图中,右键应始终执行相同的操作。

我使用下面的代码试着说如果buttonIndex == 1,做点什么。

目前,它仅适用于我的某个警报视图。其他人最终关闭了警报视图,从未运行IF buttonIndex == 1的代码。

任何想法都会受到赞赏。

if (a==1) {
            NSString *message = [[NSString alloc] initWithFormat:
                             @"Only $%@!",dollas.text];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                              @"Really?!"

                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Close"
                                              otherButtonTitles:@"Facebook",nil];
        [alert show];
        [alert release];
        [message release];

        }

else if (a==2) {
            NSString *message = [[NSString alloc] initWithFormat:
                                 @"Somone just paid you $%@", dollas.text];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                                  @"Swish!"

                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:@"Close"
                                                  otherButtonTitles:@"Facebook",nil];
            [alert show];
            [alert release];
            [message release];

        }

代表:

- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1)
    {
       do.stuff;
    }

3 个答案:

答案 0 :(得分:4)

您应该将委托设置为self,以便调用该方法。

IE -

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                      @"Really?!"

                                                message:message
                                               delegate:self //SELF
                                      cancelButtonTitle:@"Close"
                                      otherButtonTitles:@"Facebook",nil];

答案 1 :(得分:2)

在每个alertview上设置标记,并在-didDismissWithButtonIndex内部首先检查alert标记

例如:

    if (a==1) {
        NSString *message = [[NSString alloc] initWithFormat:
                         @"Only $%@!",dollas.text];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                          @"Really?!"

                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"Close"
                                          otherButtonTitles:@"Facebook",nil];
    alert.tag = 1;
    [alert show];
    [alert release];
    [message release];

    }
    else if (a==2) {
        NSString *message = [[NSString alloc] initWithFormat:
                             @"Somone just paid you $%@", dollas.text];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                              @"Swish!"

                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"Close"
                                              otherButtonTitles:@"Facebook",nil];
        alert.tag = 2;
        [alert show];
        [alert release];
        [message release];

    }

然后在-didDismissWithButtonIndex

- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1 && actionSheet.tag == 1)
    {
       do.stuff;
    }
    else if (buttonIndex == 1 && actionSheet.tag == 2)
    {
       do.otherStuff;
    }

答案 2 :(得分:2)

对于案例(a == 2),您将UIAlertView委托设置为nil,因此在这种情况下甚至不应调用- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex。更改它以将委托设置为自己。