iOS 引用當(dāng)前顯示的UIAlertView

UIAlertView在iOS里和一般的UIView不一樣,有時候使用起來會有一些不便箕母。特別要引用當(dāng)前顯示的UIAlertView的時候,就存在一些難度。

在iOS7以前,可以下面的代碼可以解決這個問題

#pragma mark 查找當(dāng)前界面有沒有一個AlertView
+(BOOL)isAlert{
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        NSArray* subviews = window.subviews;
        if ([subviews count] > 0)
            if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
                return YES;
    }
    return NO;
}
#pragma mark 關(guān)閉當(dāng)前界面上的alertView
+(void)closeAlert{
    for (UIWindow* window in [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];
    }
}

可以把它放在一個公用的類中作為靜態(tài)方法調(diào)用锅移,使用起來非常方便。

不幸的是饱搏,iOS7以后不能使用了非剃。事實(shí)上,在iOS7以后推沸,UIAlertView已經(jīng)不屬于任何一個window了备绽,-[UIAlertView window]的值一直是nil。 而且alert view 的管理方法在開發(fā)文檔里也沒有列出來鬓催。這意味著肺素,即使遍歷整個windows的subviews,也找不到AlertView宇驾。

判斷當(dāng)前keyWindow

/// 查找當(dāng)前界面有沒有一個AlertView.
+(BOOL)isAlert{
    if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
    {
        ////There is no alertview present
        return  NO;
    }
    return YES;
}

這個方法看起來是比較簡單的倍靡,可惜無法引用到UIAlertView,就無法用代碼關(guān)閉它课舍。我嘗試用

if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
////There is no alertview present
return ;
}
UIAlertView* alert=(UIAlertView*)[UIApplication sharedApplication].keyWindow;

這樣的代碼塌西,但是失敗了他挎。

國外有提出下面的iOS7處理方案:

Class UIAlertManager = objc_getClass("_UIAlertManager");
UIAlertView *topMostAlert = [UIAlertManager performSelector:@selector(topMostAlert)];

我沒有成功運(yùn)行這個代碼,最重要原因我希望能寫一個公用的方法獲取當(dāng)前的UIAlertView雨让,所以這個方法我不感興趣雇盖。
上面代碼也可以這樣寫:

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

感興趣的同學(xué)可以試一下。但據(jù)說這個方法Apple Store是不會審核通過的栖忠,因?yàn)樗玫降氖俏垂_的方法崔挖。

在當(dāng)前ViewController定義一個isAlertView變量

這個方法原理比較簡單,但使用起來挺麻煩庵寞。

// initialize default flag for alert... If alert is not open set isOpenAlert as NO
BOOL isAlertOpen;
isAlertOpen = NO;
if (isAlertOpen == NO) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert is Open" delegate:self cancelButtonTitle:@"Okay!!" otherButtonTitles: nil];
    [alert show];
    // Now set isAlertOpen to YES
    isAlertOpen = YES;
}
else
{
    //Do something
}

使用Notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aWindowBecameVisible:) name:UIWindowDidBecomeVisibleNotification object:nil];

然后在aWindowBecameVisible里驗(yàn)證:

if ([[theWindow description] hasPrefix:@"<_UIModalItemHostingWin"])
{
    // This is the alert window
}

這個方法也有點(diǎn)麻煩狸相。

在AppDelegate定義公用變量

在AppDelegate.h里:

@property (nonatomic, assign) BOOL isAlertVisibleOnAppWindow;

在使用UIAlertView的時候:

AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
if (!delegate.isAlertVisibleOnAppWindow) {
    delegate.isAlertVisibleOnAppWindow = YES;

    UIAlertView *alertView = [[UIAlertView alloc] init…//alert init code

}

在按鈕的點(diǎn)擊事件里,要給isAlertVisibleOnAppWindow再賦值捐川。

這方法也不容易脓鹃。

自定義一個MyUIAlertView

使用一個static BOOL alertIsShowing變量,然后override -(void)show selector.
在show的時候古沥,就可以判斷當(dāng)前alertIsShowing的值瘸右。
而且可以自己定義一個close方法。

UIAlertController

蘋果官方文檔介紹岩齿,UIAlertView在iOS8以后不贊成再繼續(xù)使用太颤,同樣UIAlertViewDelegate可能也要廢棄了。使用UIAlertController來替代UIAlertView盹沈。關(guān)于UIAlertController的用法我在下一篇博文里介紹龄章,這里還是嘗試能否查找到現(xiàn)有UIAlertController。下面的代碼經(jīng)測試可以成功運(yùn)行:

@implementation StringUtils
/// 查找當(dāng)前界面有沒有一個AlertView.
+(BOOL)isAlert{
    if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
    {
        return  NO;
    }
    return YES;
}

/// 關(guān)閉當(dāng)前界面上的alertView.
+(void)closeAlert{
    UIViewController* c=[self activityViewController];
    if([c isKindOfClass:[UIAlertController class]]){
        NSLog(@"success");
    }
    else if([c isKindOfClass:[UINavigationController class]]){
        UINavigationController* d =(UINavigationController*)c;

        if([d.visibleViewController isKindOfClass:[UIAlertController class]]){
            UIAlertController* control=(UIAlertController*)d;
            [control dismissViewControllerAnimated:YES completion:^{}];
            NSLog(@"success again");
        }
    }
}
/// 查找當(dāng)前活動窗口.
+ (UIViewController *)activityViewController
{
    UIViewController* activityViewController = nil;

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if(window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] 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];

        id nextResponder = [frontView nextResponder];

        if([nextResponder isKindOfClass:[UIViewController class]])
        {
            activityViewController = nextResponder;
        }
        else
        {
            activityViewController = window.rootViewController;
        }
    }

    return activityViewController;
}
@end

調(diào)用時乞封,使用

[StringUtils closeAlert];

即可關(guān)閉當(dāng)前打開的UIAlertController窗口做裙。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市肃晚,隨后出現(xiàn)的幾起案子锚贱,更是在濱河造成了極大的恐慌,老刑警劉巖关串,帶你破解...
    沈念sama閱讀 211,423評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惋鸥,死亡現(xiàn)場離奇詭異,居然都是意外死亡悍缠,警方通過查閱死者的電腦和手機(jī)卦绣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,147評論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來飞蚓,“玉大人滤港,你說我怎么就攤上這事。” “怎么了溅漾?”我有些...
    開封第一講書人閱讀 157,019評論 0 348
  • 文/不壞的土叔 我叫張陵山叮,是天一觀的道長。 經(jīng)常有香客問我添履,道長屁倔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,443評論 1 283
  • 正文 為了忘掉前任暮胧,我火速辦了婚禮锐借,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘往衷。我一直安慰自己钞翔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,535評論 6 385
  • 文/花漫 我一把揭開白布席舍。 她就那樣靜靜地躺著布轿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪来颤。 梳的紋絲不亂的頭發(fā)上汰扭,一...
    開封第一講書人閱讀 49,798評論 1 290
  • 那天,我揣著相機(jī)與錄音福铅,去河邊找鬼东且。 笑死,一個胖子當(dāng)著我的面吹牛本讥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鲁冯,決...
    沈念sama閱讀 38,941評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼拷沸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了薯演?” 一聲冷哼從身側(cè)響起撞芍,我...
    開封第一講書人閱讀 37,704評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎跨扮,沒想到半個月后序无,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,152評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衡创,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,494評論 2 327
  • 正文 我和宋清朗相戀三年帝嗡,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片璃氢。...
    茶點(diǎn)故事閱讀 38,629評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡哟玷,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出一也,到底是詐尸還是另有隱情巢寡,我是刑警寧澤喉脖,帶...
    沈念sama閱讀 34,295評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站抑月,受9級特大地震影響树叽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谦絮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,901評論 3 313
  • 文/蒙蒙 一题诵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧挨稿,春花似錦仇轻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至臭家,卻和暖如春疲陕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背钉赁。 一陣腳步聲響...
    開封第一講書人閱讀 31,978評論 1 266
  • 我被黑心中介騙來泰國打工蹄殃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人你踩。 一個月前我還...
    沈念sama閱讀 46,333評論 2 360
  • 正文 我出身青樓诅岩,卻偏偏與公主長得像,于是被迫代替她去往敵國和親带膜。 傳聞我的和親對象是個殘疾皇子吩谦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,499評論 2 348

推薦閱讀更多精彩內(nèi)容