UIAlerView显示后添加OK按钮

Add OK button to UIAlerView after it is displayed

看了这个 post 但它不是我想要的。

adding ok button at run time in UIAlertview ios

我显示的 UIAlertview 没有按钮,我说 'waiting for event to happen'。如果该事件没有发生或发生了,我想使用相同的警报视图来通知该事件已完成,然后单击 'ok' 关闭该警报。如何在显示 UIAlertview AFTER 后添加按钮?或者当偶数发生时,我应该用以下方式忽略这个:

[waitForEvent dismissWithClickedButtonIndex:0 animated:YES];
waitForEvent = nil;

并显示一个新的 UIAlert。

试试这个:

UIAlertController *waitForEvent = [UIAlertController alertControllerWithTitle:nil message:@"waiting for event to happen" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:waitForEvent animated:YES completion:nil];

// wait for five seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil];
  [waitForEvent addAction:action];
});