2010-09-25 91 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