2

我使用警报视图显示活动指示器:

 UIAlertView *Alert= [[UIAlertView alloc] initWithTitle:@"Loading"
                                               message:@"\n"
                                              delegate:self
                                     cancelButtonTitle:nil
                                     otherButtonTitles:nil];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
spinner.center = CGPointMake(139.5, 75.5); 
[Alert addSubview:spinner];
[spinner startAnimating];
[Alert show];    

[Alert dismissWithClickedButtonIndex:0 animated:YES];

唯一的事情是它很快就会从屏幕上消失,我想指定警报在屏幕上停留的时间,比如两秒钟,而不是现在显示的纳秒。我不确定如何进行/实现这一点,我会使用某种 NSTimer,如果是的话,我将非常感谢一些如何实现这一点的建议。谢谢。

4

1 回答 1

1

这是我将如何使用NSTimerand解决问题的示例NSInvocation。我没有测试它,所以可能会遇到一些问题。

UIAlertView *Alert= [[UIAlertView alloc] initWithTitle:@"Loading"
                                               message:@"\n"
                                              delegate:self
                                     cancelButtonTitle:nil
                                     otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
spinner.center = CGPointMake(139.5, 75.5); 
[Alert addSubview:spinner];
[spinner startAnimating];
[Alert show];  
NSMethodSignature *mySignature = [UIAlertView instanceMethodSignatureForSelector:@selector(dismissWithClickedButtonIndex:animated:)];
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:mySignature];
[myInvocation setSelector:@selector(dismissWithClickedButtonIndex:animated:)];
[myInvocation setTarget:Alert];
int buttonIndex = 0;
bool isAnimated = YES;
[myInvocation setArgument:&buttonClicked 
                  atIndex:2];
[myInvocation setArgument:&isAnimated 
                  atIndex:3];
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:2 
                                          invocation:myInvocation 
                                             repeats:NO];  

我仍然认为这不是问题的最佳答案。在大多数情况下,您应该使用指标直到要完成的工作完成,而不是基于时间,尤其不是可以改变或依赖于其他因素的时间。

所以正确的答案是等到工作完成,然后关闭UIAlertView.

于 2012-07-12T19:35:24.047 回答