0

我在它自己的委托中调用 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:

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

4

1 回答 1

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

于 2014-05-22T22:17:00.673 回答