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 秒。所以在 UI 中看到该指示器是正常的。但什么也没发生!有什么想法吗?
Here is my code:
-(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
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要向这样的发送者声明您的方法
You need to declare your method with a sender like this
一切都被执行了。您的问题是
saveImageToDisk
调用是同步的,并且您是从 UI 线程调用它。当您阻塞 UI 线程时,不会重新绘制任何内容。指示器已显示,但无法将其绘制到屏幕上,直到 IBAction 返回(此时它将再次隐藏)。您必须异步调用保存方法。
阻塞 UI 线程从来都不是一个好主意。
编辑:请参阅以下问题的答案以获得正确的解决方案: asynchronous requests to database in ios
Edit2:可能的解决方案之一(未测试)
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 theIBAction
returns when it will have been hidden again.You have to call the saving method asynchronously.
Blocking UI thread is never a good idea.
Edit: 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)
您确定
1)indicatorView 和 statusLabel 不为 null,并且
2)indicatorView 和 statusLabel 作为子视图添加到 self.view 中吗?
Are you sure that
1) indicatorView and statusLabel are not null, and
2) indicatorView and statusLabel are added as subviews to self.view?
在我的看法中,您正在启动活动指示器主线程。
相反,在主线程上显示指示器,
您应该在单独的线程上调用指示器,如下所示。
In My Perception your are starting the Activity Indicator main Thread.
Instead Showing the Indicator on main thread
you should call Indicator on seperate Thread as Below.