在委托中显示UIAlertView失败

时间:2014-05-22 22:04:41

标签: ios uialertview uialertviewdelegate

我在其中调用了UIAlertView自己的委托,但它失败了。代码很简单:

@interface ViewController () <UIAlertViewDelegate>
@property (nonatomic, strong) UIAlertView *alertView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.alertView = [[UIAlertView alloc] initWithTitle:@"Howdy"
                                                message:@"Here's the alert"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
    [self.alertView show]; // this shows the 
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self.alertView show]; // this does not show the alert again!
    }
}

@end

但是,当我删除时:

[self.alertView show] 

并将其替换为:

[self.alertView performSelector:@selector(show) withObject:nil afterDelay:0.01]

它有效。

这似乎表明即使我在委托方法UIAlertVIew中,原始的alertView:didDismissWithButtonIndex:也没有完全被驳回。

虽然这是有效的,但似乎并不正确。谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:2)

你可能是对的,但我不明白为什么你会再次出现同样的警报。由于您通常不需要保留对警报的引用,因此您可以编写如下方法:

- (void)showAlert {
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Howdy"
                                                   message:@"Here's the alert"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"OK", nil];
    [alert show];
}

如果您从viewDidAppear而不是viewDidLoad拨打此电话也会更好。

相关问题