2011-09-28 36 views
2

我正在編寫應用程序測試,並且想要檢查當前是否顯示UIAlertView。這意味着我沒有指向它的指針,我需要知道顯示的UIAlertView的所有者是誰,或者可以在哪些視圖堆棧中找到它。查找UIAlertView而不參考它

+1

可能重複:[iPhone SDK:檢查是否一個UIAlertView中被示出](HTTP://計算器。 com/questions/2528929/iphone-sdk-check-if-a-uialertview-is-showing) –

+0

謝謝。檢查應用程序的窗口是解決方案。沒有考慮到有幾個窗口。僅檢查由應用程序委託人控制的人員。 – Gamadril

回答

8

[UPDATE]實際上,下面的解決方案不適用於iOS 7,因爲警報視圖不會再綁定到特定的窗口。在這裏閱讀詳細信息:https://stackoverflow.com/a/18703524/942107

基於KennyTM'm另一個問題的建議Sebastien提供了鏈接,我創建了一個UIAlertView類別,並帶有返回UIAlertView的方法,如果能夠以編程方式解除它的話。

UIAlertViewAdditions.h:

#import <UIKit/UIKit.h> 

@interface UIAlertView (UIAlertViewAdditions) 

+ (UIAlertView *) getUIAlertViewIfShown; 

@end 

UIAlertViewAdditions.m:

#import "UIAlertViewAdditions.h" 

@implementation UIAlertView (UIAlertViewAdditions) 

+ (UIAlertView *) getUIAlertViewIfShown { 
    if ([[[UIApplication sharedApplication] windows] count] == 1) { 
     return nil; 
    } 

    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    if ([window.subviews count] > 0) { 
     UIView *view = [window.subviews objectAtIndex:0]; 
     if ([view isKindOfClass:[UIAlertView class]]) { 
      return (UIAlertView *) view; 
     } 
    } 
    return nil; 
} 

@end 
+1

我在我的項目中使用這個代碼,現在它不在iOS7中工作:( –

+0

嘗試'UIWindow * window = [[[UIApplication sharedApplication] windows] objectAtIndex:2];''和'UIView * view = [window.subviews objectAtIndex :1];'但更簡潔的方法是檢查類名 – Gamadril

+2

不幸的是,這並不能解決ios7的錯誤 – Lukas