iOS进度条未更新

iOS进度条未更新,ios,objective-c,iphone,cocoa-touch,Ios,Objective C,Iphone,Cocoa Touch,我的进度条出现问题,没有更新(它会变成灰色)。我使用此方法更新MainViewController中的进度条。m: - (void)setProgress:(float)prog { NSLog(@"Progressbar updated, value %f", prog); self.progressBar.progress = prog; } 当我使用以下命令从类内部(MainViewController.m内部)调用它时: 一切都很顺利,蓝色的进度条显示了进度,我得到了

我的进度条出现问题,没有更新(它会变成灰色)。我使用此方法更新MainViewController中的进度条。m:

- (void)setProgress:(float)prog {

    NSLog(@"Progressbar updated, value %f", prog);
    self.progressBar.progress = prog;
}
当我使用以下命令从类内部(MainViewController.m内部)调用它时:

一切都很顺利,蓝色的进度条显示了进度,我得到了有价值的NSLog输出

使用此进度条,我希望可视化我在不同类(processing.m)中运行的循环中所做的处理,以将带有进度的消息发送到我使用协议的MainViewController:

#import "progressProtocol.h"

@interface MainViewController : UIViewController <progressProtocol>
- (void)setProgress:(float)prog;

@end
当我通过协议发送消息来调用setProgress方法时,我只得到具有正确值的NSLog输出,但进度条没有根据值移动!?当我在方法内部设置断点时,我可以看到该方法被调用,但进度条没有移动


在循环内部,我将使用新值发送消息大约数百次,但在进度条上没有任何更改,只有NSLog使用正确的值。有什么想法吗?

听起来你在做这样的事情,在主线程上:

int steps = 10000
for (int step = 1; x < steps; x++) {
  //Do something time-consuming that takes a few seconds.
  viewController.setProgress((float)step/(float)steps)
}
int steps=10000
对于(int step=1;x
那不行

iOS中的UI更新仅在代码返回并访问事件循环时发生

如果有一个循环在主线程上执行耗时的任务,并且在完成任务之前不会返回,那么更新进度指示器的代码就永远没有机会更新UI

相反,您可能应该编写代码在后台线程上执行耗时的工作:

//Run the time-consuming loop on a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
 ^{
    int steps = 10000
    for (int step = 1; x < steps; x++) {
      //Do something time-consuming that takes a few seconds.

      //As the loop progresses, send a message to update the progress 
      //indicator to the main thread.
      dispatch_async(dispatch_get_main_queue(),
      ^{
        viewController.setProgress((float)step/(float)steps)
      }
    }
  }
//在后台队列上运行耗时的循环
调度异步(调度获取全局队列(调度队列优先级默认为0),
^{
整数步数=10000
对于(int step=1;x
使用协议调用时,检查
prog
的值是多少。主线程上的条是否更新?必须更新。@Frankie:我让NSLog也打印prog
NSLog的值(@“Progressbar updated,value%f”,prog);
并且传递的值正确!信息不足。什么代码称为setProgress?它在循环中吗?该代码是在主线程上运行还是在后台线程上运行?你是什么意思"当我通过协议调用它时?还有什么其他对象正在将消息发送到您的视图控制器?您需要在调用上述方法的位置发布其他代码,由于信息有限,您无法期望得到任何帮助。我已经根据您的方案设置了它,进度条现在正在更新,但经过一定的更新处理崩溃的百分比:
malloc:**对象0x1008b6a00错误:已释放对象的校验和不正确-对象可能在被释放后被修改。
以前处理工作正常-您知道哪里可以找到导致崩溃的原因吗?与多线程程序相关的各种潜在问题g、 在我看来,在长时间运行的流程仍在运行的情况下,有些东西正在被释放。请在最后用新代码更新您的问题,解释长时间运行的代码在做什么。
int steps = 10000
for (int step = 1; x < steps; x++) {
  //Do something time-consuming that takes a few seconds.
  viewController.setProgress((float)step/(float)steps)
}
//Run the time-consuming loop on a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
 ^{
    int steps = 10000
    for (int step = 1; x < steps; x++) {
      //Do something time-consuming that takes a few seconds.

      //As the loop progresses, send a message to update the progress 
      //indicator to the main thread.
      dispatch_async(dispatch_get_main_queue(),
      ^{
        viewController.setProgress((float)step/(float)steps)
      }
    }
  }