访问多个UIAlertViews上的边界时出现问题

时间:2010-09-25 05:09:02

标签: iphone objective-c uialertview

我正在使用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的原因?

1 个答案:

答案 0 :(得分:1)

我不确定为什么你会在警报上获得任何正确的界限 - UIAlertView的帧在显示之前计算

I've had this issue before。就我而言,我正在查看UIAlertView的按钮大小,它的宽度和高度始终为0.

我能够使用UIAlertViewDelegate获取正确的帧信息并实现

- (void)willPresentAlertView:(UIAlertView *)alertView

如果您无法使用委托重构代码,我的下一个最佳建议是推送您自己的UIAlertView类。 github上有一些好的,比如TSAlertView