Ron*_*Ron 4 iphone uialertview ios4 ios
仍在尝试在警报处于活动状态时更新UIAlertview中的消息.我删除了问题的大部分内容,并在下面的更新中发布了更多代码.
更新3:添加更多代码!在.h文件中,我声明了以下内容(以及其他内容):
@interface WebViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *webView;
UIAlertView *alert;
}
Run Code Online (Sandbox Code Playgroud)
我@property和@synthesize UIAlertview来.
接下来,我在IBAction中创建警报,该按钮单击按钮运行:
-(IBAction)convert {
convertButton.enabled = NO;
mailButton.enabled = NO;
backButton.enabled = NO;
//show the alert window
alert = [[UIAlertView alloc] initWithTitle:@"Converting in progress\nPlease Wait..." message:@"\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
[alert setMessage:@"getting roster"];
}
Run Code Online (Sandbox Code Playgroud)
然后它跳转到以下函数:
- (void)didPresentAlertView:(UIAlertView *)progressAlert {
//A lot of code
[alert setMessage:@"Checking history"];
//more code
[alert setMessage:@"writing history"];
//even more code
[alert setMessage:@"converting roster"];
}
Run Code Online (Sandbox Code Playgroud)
didPresentAlertView方法以ASIHTTPRequest结束以将数据发布到网页,当此请求完成时,代码最终跳转到最后一个方法以退出UIAlertView并关闭所有内容:
- (void)requestFinished:(ASIHTTPRequest *)request {
[timer invalidate];
[alert dismissWithClickedButtonIndex:0 animated:YES];
backButton.enabled = YES;
[alert release];
}
Run Code Online (Sandbox Code Playgroud)
我还从我的UIAlertView init中删除了自动释放,以确保在剩余的过程中它存在.
就像现在一样,代码只会触发第一个setMessage - >'获取名单'和最后一个 - >'转换名单'.中间的setMessage请求不会被解雇..
真的希望有人可以帮助我!
谢谢大家!
现在我看到了问题.
更新message属性时,它不会立即重新绘制视图.视图标记为"需要绘制",实际绘图稍后发生,通常在主线程中当前或下一个runloop的末尾.
因此,当您的didPresentAlertView方法在主线程上运行时,在方法完成之前不会重新绘制警报视图.这就是为什么计算密集型作业需要在单独的线程上运行以增加UI的响应性,因为UI相关作业是在主线程上完成的.
你应该做的就是运行//A lot of code //more code,并//even more code在一个单独的线程,并更新message在主线程性能.
例如,您的代码可能类似于:
// this is inside didPresentAlertView method
NSOperationQueue* aQueue = [[NSOperationQueue alloc] init];
[aQueue addOperationWithBlock: ^{
// A lot of code
[alert performSelector:@selector(setMessage:) onThread:[NSThread mainThread]
withObject:@"Checking history" waitUntilDone:NO];
// more code
[alert performSelector:@selector(setMessage:) onThread:[NSThread mainThread]
withObject:@"writing history" waitUntilDone:NO];
// it goes on
}];
Run Code Online (Sandbox Code Playgroud)
如果您正在使用iOS 4.0及更高版本,并且想要使用GDC,请小心,因为它可能会检测您的计算和消息更新的独立性,并让它们同时发生(这在此不需要).