没有按钮的 UIAlertView 以及如何关闭它

Posted

技术标签:

【中文标题】没有按钮的 UIAlertView 以及如何关闭它【英文标题】:UIAlertView with NO buttons and how to close it 【发布时间】:2012-11-14 13:06:13 【问题描述】:

在我的应用程序获得服务器响应之前,我在警报视图中有活动指示器。该应用程序将数据发送到服务器,并且警报视图显示,当服务器向我发送响应时如何关闭它。这是我的警报中的代码

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Canceling reservation" message:@"please wait" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
        [alert show];

        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

        // Adjust the indicator to place at the bottom of the dialog window.
        indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height-50);
        [indicator startAnimating];
        [alert addSubview:indicator];

【问题讨论】:

【参考方案1】:
[alert dismissWithClickedButtonIndex:0 animated:YES];

【讨论】:

【参考方案2】:

您可以使用MBProgressHUD 代替标准的 UIAlertView 来完成类似的操作。

【讨论】:

【参考方案3】:

您可以使用dismissWithClickedButtonIndex: 委托方法来关闭alertView。

[alert dismissWithClickedButtonIndex:0 animated:YES];

确保在@Interface 上声明了alert

dismissWithClickedButtonIndex:animated:

关闭接收器,可选择使用动画。

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated Parameters

按钮索引

The index of the button that was clicked just before invoking this method. The button indices start at 0.

动画

YES if the receiver should be removed by animating it first; otherwise, NO if it should be removed immediately with no animation.

讨论

ios 4.0 中,您可能希望在应用程序的任何时候调用此方法 移动到背景。警报视图不会自动关闭 当应用程序移动到后台时。这种行为不同 来自以前版本的操作系统,它们所在的位置 应用程序终止时自动取消。解雇 警报视图使您的应用程序有机会保存更改或 中止操作并执行任何必要的清理,以防您的 应用程序稍后终止。可用性

Available in iOS 2.0 and later.

在 UIAlertView.h 中声明

请参考UIAlertView

【讨论】:

以上是关于没有按钮的 UIAlertView 以及如何关闭它的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertView 操作和取消按钮

带有单选按钮 iOS 的 UIAlertView [关闭]

UIAlertView 按钮操作

删除 applicationDidEnterBackground 中的 uialertview

UIAlertView:点击警报框外部时关闭 UIAlertView [重复]

阻止 UIAlertView 关闭