Xcode 6.4, for iOS8.4, ARC enabled
There are many posts on this topic. It seemed to me that this is not a clear solution, so I spent several hours of testing and finally put together a solution that helps solve the OP problem:
"I'm trying to dismiss the UIAlertView before showing to others ..."
As stated in the OP, the ".windows" method will no longer work. There are some other ways that I read about this, including creating a category for UIAlertView and others using notifications; however they were too complicated for me.
Here's what to do ...
1) Match your class with UIAlertViewDelegate.
In your "* .h" file of your class ...
@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 using the following method:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated:
A word wise, you do not have to, in some situations your class is compatible with UIAlertViewDelegate, but it is a safer choice. It all depends on how you use your object in your class.
2) Declare the UIAlertView objet as a class variable or as a property.
Some of the advantages of creating a property are that you can have access to certain getters and setters of the object.
As an instance variable in your "* .h" file of your class ...
@interface YourViewController : UIViewController <UIAlertViewDelegate> { UIAlertView *yourAlertView; {
As a property (recommended) in your "* .h" file of your class ...
@interface YourViewController : UIViewController <UIAlertViewDelegate> { //other instance variables { @property (strong, nonatomic) UIAlertView *yourAlertView; @end
3) Avoid creating multiple links to your UIAlertView object.
For example, if you have a method that monitors a specific condition and displays a warning, then do not create a UIAlertView object each time. Instead, create it once in -(void)viewDidLoad and use it where you need it. Otherwise, it will prevent
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated:
from sending the desired message to the correct UIAlertView object.
4) Assign the tag to the UIAlertView object and process 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) Reject until the modified UIAlertView object is displayed.
self.yourAlertView.title = @"some other title string"; self.yourAlertView.message = @"some other message string"; [self.yourAlertView show];
7) UIAlertView depreciates 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 alerts and manage them in iOS 8 and later, use the UIAlertController with preferredStyle UIAlertControllerStyleAlert instead.
In applications running on versions of iOS prior to iOS 8, use the UIAlertView Class to display a warning message to the user. Warning to view functions similar, but different in appearance from the action sheet (an instance of the UIActionSheet).
Use the properties and methods defined in this class to set the title, message, and delegate from the alert view and customize the buttons. You must have a delegate installed if you add custom buttons. The delegate must conform to the UIAlertViewDelegate protocol. Use the show method to display a warning view after you configure it.