IOS references the currently displayed Uialertview

Source: Internet
Author: User

Uialertview is not the same in iOS as in general UIView, and sometimes it can be inconvenient to use. In particular, there are some difficulties in referencing the currently displayed Uialertview.

Uialertview before IOS7

The following code can be used to solve this problem:

#pragma mark find the current interface there is a alertview+ (BOOL) isalert{ for(uiwindow* windowinch[UIApplication sharedapplication].windows) {nsarray* subviews = window.subviews;if([Subviews Count] >0)if([[Subviews objectatindex:0] Iskindofclass:[uialertview class])returnYES; }returnNO;} #pragma mark closes alertview+ (void) on the current interface closealert{ for(uiwindow* windowinch[UIApplication sharedapplication].windows) {nsarray* subviews = window.subviews;if([Subviews Count] >0)if([[Subviews objectatindex:0] Iskindofclass:[uialertview class])[[Subviews objectatindex:0] dismisswithclickedbuttonindex:0 Animated:yes]; }}

You can put it in a common class as a static method call, which is very convenient to use. Unfortunately, iOS7 cannot be used later. In fact, after IOS7, Uialertview no longer belongs to any window,-[uialertview window's value has been nil. And the Alert view management method is not listed in the development documentation. This means that Alertview is not found even if you traverse the subviews of Windows.

Judging the current Keywindow
/// 查找当前界面有没有一个AlertView.+(BOOL)isAlert{    if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])    {        ////There is no alertview present        return  NO;    }    return YES;}

This method seems to be relatively simple, but unfortunately cannot be referenced to Uialertview, you cannot close it with code. I'll try it.

if ([[[UIApplication Sharedapplication].keywindow Ismemberofclass:[uiwindow class]])
{
There is no alertview present
return;
}
uialertview* alert= (uialertview*) [UIApplication Sharedapplication].keywindow;

Such a code, but failed.

Abroad has proposed the following iOS7 treatment scheme:
Class UIAlertManager = objc_getClass("_UIAlertManager"*topMostAlert = [UIAlertManager performSelector:@selector(topMostAlert)];

I did not run this code successfully, the most important reason I want to be able to write a common method to get the current Uialertview, so this method I am not interested.
The above code can also be written like this:

UIAlertView *topMostAlert = [NSClassFromString(@"_UIAlertManager"performSelector:@selector

Interested students can try. However, it is said that the Apple store does not approve this method because it uses an undisclosed method.

Defines a Isalertview variable in the current Viewcontroller

The principle of this method is relatively simple, but it is very troublesome to use.

// initialize default flag for alert... If alert is not open set isOpenAlert as NOBOOLNO;ifNO) {    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert is Open" delegate:self cancelButtonTitle:@"Okay!!"nil];    [alert show];    // Now set isAlertOpen to YES    YES;}else{    //Do something}
Using notification
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(aWindowBecameVisible:) name:UIWindowDidBecomeVisibleNotification object:nil];

Then verify in awindowbecamevisible:

ifhasPrefix:@"<_UIModalItemHostingWin"]){    //iswindow}

This method is also a bit troublesome.

Defining common variables in Appdelegate

In the AppDelegate.h:

@property (nonatomicassignBOOL isAlertVisibleOnAppWindow;

When using the Uialertview:

AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;if (!delegate.isAlertVisibleOnAppWindow) {    delegate.isAlertVisibleOnAppWindow = YES;    UIAlertView *alertView = [[UIAlertView alloc] init…//alert init code}

In the button click event, you want to assign a value to the Isalertvisibleonappwindow.

This method is not easy either.

Customizing a Myuialertview

Use a static BOOL alertisshowing variable, and then override-(void) show selector.
At show time, you can judge the value of the current alertisshowing.
And you can define a close method yourself.

Uialertcontroller

Apple Official document, Uialertview after iOS8 not agree to continue to use, the same uialertviewdelegate may also be scrapped. Use Uialertcontroller to replace Uialertview. About the usage of Uialertcontroller I it in the next article, this is still trying to find the existing uialertcontroller. The following code has been tested to run successfully:

 @implementation stringutils /// Find the current interface there is no one alertview.+(BOOL) isalert{if([[uiapplicationSharedapplication]. Keywindowismemberofclass:[UIWindowClass]]) {return  NO; }return YES;}/// Close the Alertview on the current interface.+(void) closealert{Uiviewcontroller* c=[ SelfActivityviewcontroller];if([C Iskindofclass:[uialertcontroller class]]) {NSLog(@"Success"); }Else if([C iskindofclass:[UinavigationcontrollerClass]]) {Uinavigationcontroller* d = (Uinavigationcontroller*) C;if([D. VisibleviewcontrollerIskindofclass:[uialertcontroller class]] {uialertcontroller* control= (uialertcontroller*) D; [Control dismissviewcontrolleranimated:YEScompletion:^{}];NSLog(@"Success Again"); }    }}/// Find the currently active window.+ (Uiviewcontroller*) activityviewcontroller{Uiviewcontroller* Activityviewcontroller =Nil;UIWindow*window = [[uiapplicationSharedapplication] Keywindow];if(Window. Windowlevel! = Uiwindowlevelnormal) {Nsarray*windows = [[uiapplicationSharedapplication] windows]; for(UIWindow*tmpwin in Windows) {if(Tmpwin. Windowlevel= = uiwindowlevelnormal) {window = Tmpwin; Break; }        }    }Nsarray*viewsarray = [Window subviews];if([Viewsarray Count] >0)    {UIView*frontview = [Viewsarray objectatindex:0];IDNextresponder = [Frontview Nextresponder];if([Nextresponder iskindofclass:[UiviewcontrollerClass]]) {activityviewcontroller = Nextresponder; }Else{activityviewcontroller = window. Rootviewcontroller; }    }returnActivityviewcontroller;}@end

When invoked, use the

[StringUtils Closealert];

To close the currently open Uialertcontroller window.

IOS references the currently displayed Uialertview

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.