ios - 将 buttonIndex 与多个警报 View 一起使用

标签 ios uialertview

当它们出现时,我有大约 4 个具有不同标准的警报 View 。在所有 4 个 View 中,右键应该总是做同样的事情。

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

目前,它仅适用于我的一个警报 View 。其他人只是最终关闭警报 View 并且从不运行 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;
    }

最佳答案

您应该将委托(delegate)设置为 self所以该方法被调用。

IE -

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

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

关于ios - 将 buttonIndex 与多个警报 View 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17386695/

相关文章:

ios - UIAlertview exc_bad_access

ios - 未调用 clickedButtonAtIndex 方法

ios - 在 viewDidLoad 中调用 addSubView 时的奇怪行为

ios - 创建 AVPlayer 视频的动画、部分模糊

ios - 更新到 IOS 7 后应用程序崩溃

ios - 每秒重新加载 Tableview 部分会导致闪烁

ios - UIAlertView间歇性不调用委托(delegate)方法

iphone - UIAlert按钮大小

iphone - UIAlertView 单击时崩溃 - 使用 ARC

ios - Core Data中有类似sql "NOT IN"的函数吗?