IBAction中跳过了用户界面命令 [英] User Interface commands skipped in IBAction
问题描述
这是我的代码:
-(IBAction)saveDownloadedImage
{
NSLog(@"Test"); EXECUTED
indicatorView.hidden = NO; NOT EXECUTED
[indicatorView startAnimating]; NOT EXECUTED
[statusLabel setText:@"WHY?"]; NOT EXECUTED
[currentPicture setImage:[imageView image]]; EXECUTED
ImageFileManager *fileManager = [[ImageFileManager alloc] init]; EXECUTED
[fileManager saveImageToDisk:currentPicture]; EXECUTED
indicatorView.hidden = YES;
[statusLabel setText:@"Image saved successfully."]; EXECUTED
saveButton.enabled = NO; EXECUTED
}
保存过程大约需要5秒钟。因此,在用户界面中看到指示器是正常的。但是什么也没发生!知道吗?
The proccess of saving takes about 5 seconds. So it would be normal to see the indicator in the UI. But nothing happens! Any idea?
推荐答案
一切都执行了。您的问题是 saveImageToDisk
调用是同步的,并且您正在从UI线程调用它。当您阻止UI线程时,不会重绘任何内容。指示符已显示,但是直到 IBAction
返回时(如果将其再次隐藏),它就无法绘制到屏幕上。
Everything is executed. Your problem is that the saveImageToDisk
call is synchronous and you are calling it from the UI thread. When you are blocking the UI thread, nothing is ever repainted. The indicator is shown but it cannot be drawn to the screen until the IBAction
returns when it will have been hidden again.
您必须异步调用保存方法。
阻止UI线程绝不是一个好主意。
You have to call the saving method asynchronously.
Blocking UI thread is never a good idea.
编辑:请参见以下内容的答案正确解决方案的问题:在ios中对数据库的异步调用
see the answer for the following question for the correct solution: asynchronous calls to database in ios
Edit2:一种可能的解决方案(未经测试)
One of the possible solutions (not tested)
-(IBAction)saveDownloadedImage {
indicatorView.hidden = NO; //Note you can use hidesWhenStopped property for this
[indicatorView startAnimating];
[statusLabel setText:@"BECAUSE..."];
[currentPicture setImage:[imageView image]];
[NSThread detachNewThreadSelector:@selector(save) toTarget:self withObject:nil]
}
- (void)save {
@autoreleasepool {
ImageFileManager *fileManager = [[ImageFileManager alloc] init];
[fileManager saveImageToDisk:currentPicture];
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
}
}
- (void)updateUI {
indicatorView.hidden = YES;
[statusLabel setText:@"Image saved successfully."];
saveButton.enabled = NO;
}
这篇关于IBAction中跳过了用户界面命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!