Prevents multiple uialertview overlapping pops

Source: Internet
Author: User

http://www.jianshu.com/p/7ac398ef4532

The project may encounter this situation, several alertview because the logical relationship pops up, the user needs a click in order to remove all the Alertview. Or in this case, we just need to pop up a alertview on the OK.

How did the Alertview pop up? The online search for data says that every time [Alertview show] is executed, this method is created with a new window and the Alertview displayed on the window. Code validation is indeed the case.

The code verifies where the Alertview is added.

- (void) Viewdidload {[Super Viewdidload];Additional setup after loading the view, typically from a nib.UIButton *TEMPBTN = [UIButton Buttonwithtype:Uibuttontypesystem]; Tempbtn. frame =CGRectMake (100,100,100,100); Tempbtn. backgroundcolor = [Uicolor Cyancolor]; [Tempbtn AddTarget:Self action:@selector (clickbtn:) forControlEvents:UIControlEventTouchUpInside]; [Self. View ADDSUBVIEW:TEMPBTN];} - (void) Clickbtn: (UIButton *) sender{Uialertview *alert1 = [[Uialertview Alloc] Initwithtitle:@ "title1" message:@ "Message1" delegate:nil Cancelbuttontitle:@ "Cancel" Otherbuttontitles:@ "OK", Nil]; [Alert1 show]; NSLog (@ "Alert1.window =%@ Alert1.window.windowLevel =%f", Alert1. window,alert1. Window. Windowlevel); NSLog (@ "App.window =%@", App. window); NSLog (@ "Windows = =%@", [uiapplication sharedapplication]. Windows);}     

Test results:

Alert1.window = <_uialertcontrollershimpresenterwindow:0x7f9ee8c07940; frame = (00;414 736); opaque = NO; gesturerecognizers = <nsarray: 0x618000056aa0>; Layer = <uiwindowlayer: 0x6180000240a0>> alert1.window.windowLevel = 2001.000000app.window = <uiwindow: 0x7f9ee8f03f80; frame = (0 0; 414 736); autoresize = w+h; gesturerecognizers = <NSArray: Span class= "Hljs-number" >0x608000052f60>; Layer = <uiwindowlayer: 0x608000022100>> windows = = ( " <UIWindow:0x7f9ee8f03f80; frame = (0 0; 414 736); AutoResize = w+h; Gesturerecognizers = <NSArray:0x608000052f60>; Layer = <UIWindowLayer:0x608000022100>> ")         

By printing the results can be seen:
1. Alert1.window does not appear in the [UIApplication sharedapplication].windows window and Windows Relationship reference: http://www.jianshu.com/p/ Only App.window in 75befce85623,windows is the current bottommost control.
2, Alert1.window Windowlevel is 2001 than App.window, App.window windowlevel is 0, so alertview shown on the top of App.window. Questions related to Windowlevel reference: http://www.jianshu.com/p/f60471a7d935

Understand the general principle of alertview display, so we need to rely on

- (void) Clickbtn: (UIButton *) sender{Uialertview *alert1 = [[Uialertview Alloc] Initwithtitle:@ "Title1" message:@ "Message1" Delegate:Nil Cancelbuttontitle:@ "Cancel" Otherbuttontitles:@ "OK", nil]; [Alert1 show]; uialertview *alert2 = [[uialertview Alloc] initWithTitle:< Span class= "hljs-string" >@ "Title2" Message:@ "Message2" delegate: nil cancelbuttontitle:@ "Cancel" Otherbuttontitles:@" OK ", nil]; [Alert2 show]; //appdelegate *app = (appdelegate *) [UIApplication Sharedapplication].delegate;////NSLog (@ "Alert1.window =%@ Alert1.window.windowLevel =%f", Alert1.window,alert1.window.windowlevel); //NSLog (@ "App.window =%@", App.window); //NSLog (@ "Windows = =%@", [UIApplication sharedapplication].windows);}     

Want extra Alertview not to show, two methods:
1, or let him not show
2, or let him show himself and then disappear
The first kind of thing I don't feel, the logic of the show is written dead.
Then take the second kind of hand, the premise is how to get to have been shown the Alertview?

The alertview shown above is displayed on the window that the system has created for itself, but this window is not available. Then what to do.
There is such a way of thinking that all the displayed Alertview recorded in an array of their own, and then do not want to do what you do!! The key point is the timing of the recording, when the Show method is selected to execute
Idea 1:
Use the runtime method to detect the Show method and then log Alertview when executing the Show method, with the following code:
Create a single instance of record Alertview

#import <Foundation/Foundation.h>@interface AlertViewRecorder : NSObject@property (nonatomic, strong)NSMutableArray * alertViewArray;+ (AlertViewRecorder *)shareAlertViewRecorder;@end
#import"AlertViewRecorder.h"@implementationAlertviewrecorderCreate a singleton, record alertview+ (Alertviewrecorder *) sharealertviewrecorder{static Alertviewrecorder *recoder = nil; static dispatch_once_t Oncetoken; dispatch_once (&oncetoken, ^{ if (Recoder = = Nil) {recoder = [[Alertviewrecorder alloc] init];}}); return recoder;} -(instancetype) init{self = [super init]; if (self) { self. Alertviewarray = [[Nsmutablearray alloc] init];} return self ;} @end                 

Key code

#import"Uialertview+myalertview.h"#import<objc/message.h>#import"AppDelegate.h"#import"AlertViewRecorder.h"@implementationUialertview (Myalertview) + (void) load{Gets the two methods that will be exchanged method Showmethod = Class_getinstancemethod (Self@selector (show)); Method Myshowmethod = Class_getinstancemethod (Self@selector (myshow));//two methods Interchange Method_exchangeimplementations (Showmethod, Myshowmethod);} -(void) myshow{//Remove all previous alertview to eliminate nsmutablearray *array = [Alertviewrecorder sharealertviewrecorder]for (uialertview *alertview in Array) {if ([Alertview iskindofclass:[uialertview class]]) { [Alertview dismisswithclickedbuttonindex:-< span class= "Hljs-number" >1 animated:YES];}} [Array removeallobjects]; //the method of invoking itself [self myshow]; [Array Addobject:self];}  @end             

Test code is feasible;

Idea 2:
Create a taxonomy, override the Show method, call the Show method in the overridden Show method, record Alertview to the relevant array, and think about 1 almost.

Thinking 1 compared to the advantages of thinking 2, I think, when the project developed a period of time or halfway to take over the project, the idea of 1 more advantages.

If there is a mistake please pass the big God instant guidance, or have better practices, also please pointing twos, under the gratitude.
Code Connection: Https://github.com/RunOfTheSnail/MyAlertViewDemo



Wen/Xiao Yan (Jane book author)
Original link: http://www.jianshu.com/p/7ac398ef4532
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Prevents multiple uialertview overlapping pops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.