2010-09-25 90 views
3

我正在使用UIAlertViews來顯示特定的應用內消息,偶爾我的應用會顯示多個警報。當我嘗試訪問警報的界限時出現問題。訪問多個UIAlertViews邊界的問題

下面是一些代碼來說明問題。這是擺在一個全新的基於視圖的應用程序:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self makeAlert:@"Zero alert" withMessage:@"This is the zero alert"]; 

    UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"First Alert" message:@"Here is the first alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [firstAlert show]; 
    NSLog(@"first alert bounds, origin: %f, %f size: %f, %f",firstAlert.bounds.origin.x,firstAlert.bounds.origin.y,firstAlert.bounds.size.width,firstAlert.bounds.size.height); 
    [firstAlert release]; 

    UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Second Alert" message:@"Here is the second alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [secondAlert show]; 
    NSLog(@"second alert bounds, origin: %f, %f size: %f, %f",firstAlert.bounds.origin.x,firstAlert.bounds.origin.y,firstAlert.bounds.size.width,firstAlert.bounds.size.height); 
    [secondAlert release]; 

    [self makeAlert:@"Third Alert" withMessage:@"Here is the third alert."]; 
    [self makeAlert:@"Fourth Alert" withMessage:@"Here is the fourth alert."]; 
} 

- (void)makeAlert:(NSString *)makeTitle withMessage:(NSString *)makeMessage { 
    UIAlertView *newAlert = [[UIAlertView alloc] initWithTitle:makeTitle message:makeMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [newAlert show]; 
    NSLog(@"%@ alert bounds, origin: %f, %f size: %f, %f",makeTitle,newAlert.bounds.origin.x,newAlert.bounds.origin.y,newAlert.bounds.size.width,newAlert.bounds.size.height); 
    [newAlert release]; 
} 

的makeAlert說明添加到.h文件並運行這個程序,你會看到在日誌的問題。零警報將顯示0,0的原點和適當的寬度和高度(3/GS上的284,141)。所有其他警報將顯示0,0寬度,高度。

如果您註釋掉零警戒線(第一個[self makeAlert...]),則firstAlert和secondAlert將顯示正確的寬度和高度,但第三個和第四個警報將顯示0,0。

不,我的應用程序一次不顯示4個或5個警報。這只是一個例證。通過子程序(或循環)創建警報會產生這個錯誤。

我現在有一個解決方法,它涉及到抓取寬度和高度時第一次使用圖像創建警報並將它們放置在某些類變量中我保留了所以稍後可以使用它們,但這並不理想(假設所有的文字和圖片都是相同的尺寸),我還需要確保我先打電話給一張圖片。

任何想法,爲什麼我得到0,0這些電話的寬度,高度?

回答

1

我不知道爲什麼你的警報任何正確的邊界 - 只是顯示之前的UIAlertView中的幀計算。

I've had this issue before。就我而言,我一直在尋找的UIAlertView中的按鈕大小,它總是顯示爲0

我能夠使用UIAlertViewDelegate得到正確的幀信息的寬度和高度,實現

- (void)willPresentAlertView:(UIAlertView *)alertView

如果你不能重構你的代碼來使用委託,我的下一個最好的建議是推出你自己的UIAlertView類。在github上有一些好的,如TSAlertView