Xcode 6.4, 适用于iOS8.4版本,启用ARC
有许多关于此主题的帖子。但是,似乎没有一个对我来说是清晰的解决方案,因此我花了几个小时进行测试,并最终提供了一个解决OP问题的解决方案:
"我试图在显示另一个UIAlertView之前关闭一个UIAlertView..."
正如OP所述,".windows"
方法将不再起作用。还有其他一些方法涉及创建UIAlertView类别和使用通知,但对我而言都过于复杂。
以下是解决方法...
1) 使您的类符合UIAlertViewDelegate协议。
在您的类的“*.h”文件中...
@interface YourViewController : UIViewController <UIAlertViewDelegate>
这将使您的类中的UIAlertView对象能够向以下方法发送消息:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
并且你的类中的UIAlertView对象需要从以下方法接收消息:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated:
聪明的人会知道,在某些情况下,您不必将您的类符合UIAlertViewDelegate的标准,但这是更安全的选择。这完全取决于您在类中如何使用对象。
2)将UIAlertView对象声明为类变量或属性。
创建属性的一些优点是,您可以访问对象的一些getter和setter。
作为实例变量,在您的类的“*.h”文件中...
@interface YourViewController : UIViewController <UIAlertViewDelegate>
{
UIAlertView *yourAlertView;
{
@end
在您的类的“*.h”文件中,属性
(推荐)…
@interface YourViewController : UIViewController <UIAlertViewDelegate>
{
{
@property (strong, nonatomic) UIAlertView *yourAlertView;
@end
3) 避免生成多个对UIAlertView对象的引用。
例如,如果您有一个监视某个条件并显示警报的方法,则不要每次实例化UIAlertView对象。相反,在-(void)viewDidLoad
中实例化一次,并在需要时使用它。否则,这将防止
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated:
将所需消息发送到正确的UIAlertView对象的方法。
4)将标签分配给UIAlertView对象,并操作属性以更改标题、消息等。
self.yourAlertView.title = @"some title string"
self.yourAlertView.message = @"some message string"
5) 显示UIAlertView对象。
[self.yourAlertView show]
6) 在显示更改后的UIAlertView对象之前,先将其关闭。
self.yourAlertView.title = @"some other title string"
self.yourAlertView.message = @"some other message string"
[self.yourAlertView show]
7) UIAlertView在iOS8中已被弃用。
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/doc/uid/TP40006802-CH3-SW8
重要提示:UIAlertView在iOS 8中已被弃用。(请注意,UIAlertViewDelegate也已被弃用。)在iOS 8及更高版本中创建和管理警报,请改用preferredStyle为UIAlertControllerStyleAlert的UIAlertController。
在运行于iOS 8之前的版本的应用程序中,使用UIAlertView类向用户显示警报消息。警报视图类似于但外观不同于操作表(UIActionSheet的实例)。
使用此类中定义的属性和方法设置警报视图的标题、消息和委托,并配置按钮。如果添加自定义按钮,则必须设置委托。委托应符合UIAlertViewDelegate协议。使用show方法在配置完毕后显示警报视图。