Xcode 6.4, for iOS8.4, ARC enabled
There are many posts on this topic. None seemed to be the clear solution to me, so I spent a few hours testing and finally have put together a solution that works to solve the OP's problem:
"I'm trying to dismiss a UIAlertView before showing another..."
As the OP stated the ".windows"
method will no longer work. There are some other ways that I have read about that involve creating a category to UIAlertView and others using notifications; however, they were too complex for me.
Here is what to do...
1) Conform your class to UIAlertViewDelegate.
In your class' "*.h" file...
@interface YourViewController : UIViewController <UIAlertViewDelegate>
This will allow the UIAlertView object in your class to send messages to the following method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
and for the UIAlertView object in your class to receive messages from the following method:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated:
A word to the wise, you do not have to, in some situations conform your class to UIAlertViewDelegate, but it is the safer choice. It all depends on how you will use your object in your class.
2) Declare the UIAlertView objet as a class variable or as a property.
Some advantages to creating a property is that you can have access to some getters and setters of the object.
As instance variable, in your class' "*.h" file...
@interface YourViewController : UIViewController <UIAlertViewDelegate>
{
UIAlertView *yourAlertView;
{
//other properties
@end
As property (recommended) in your class's "*.h" file...
@interface YourViewController : UIViewController <UIAlertViewDelegate>
{
//other instance variables
{
@property (strong, nonatomic) UIAlertView *yourAlertView;
@end
3) Avoid generating multiple references to your UIAlertView object.
For example, if you have a method that monitors a certain condition and shows the alert, then do not instanciate the UIAlertView object each time. Instead, instantiate it once in -(void)viewDidLoad
and use it where you need it. Otherwise, this will prevent the
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated:
method from sending the desired message to the correct UIAlertView object.
4) Assign the tag to the UIAlertView object and manipulate the properties to change the title, message, etc.
self.yourAlertView.title = @"some title string";
self.yourAlertView.message = @"some message string";
5) Show the UIAlertView object.
[self.yourAlertView show];
6) Dismiss before showing changed UIAlertView object.
self.yourAlertView.title = @"some other title string";
self.yourAlertView.message = @"some other message string";
[self.yourAlertView show];
7) UIAlertView is depreciated in iOS8.
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/doc/uid/TP40006802-CH3-SW8
Important: UIAlertView is deprecated in iOS 8. (Note that
UIAlertViewDelegate is also deprecated.) To create and manage alerts
in iOS 8 and later, instead use UIAlertController with a
preferredStyle of UIAlertControllerStyleAlert.
In apps that run in versions of iOS prior to iOS 8, use the
UIAlertView class to display an alert message to the user. An alert
view functions similar to but differs in appearance from an action
sheet (an instance of UIActionSheet).
Use the properties and methods defined in this class to set the title,
message, and delegate of an alert view and configure the buttons. You
must set a delegate if you add custom buttons. The delegate should
conform to the UIAlertViewDelegate protocol. Use the show method to
display an alert view once it is configured.