【问题标题】:how to use the progress bar in the iphone app如何使用iphone应用程序中的进度条
【发布时间】:2011-09-14 07:34:14
【问题描述】:

在我的 iPhone 应用程序中,我正在从 FTP 服务器下载一些数据。为了显示我正在使用的操作UIActivityIndicator。如果我把UIProgressView 放在那里而不是UIActivityIndicator,那会更合适。下载数据时如何使用UIProgressView?谁能给我一个教程链接或示例代码?提前致谢。

【问题讨论】:

  • 你在使用 NSURLConnection- 方法吗?从 ftp 下载?
  • 在 swift 中试用这个适用于 iOS 的 HUD 库 github.com/shubh10/JustHUD

标签: iphone objective-c ios xcode uiprogressview


【解决方案1】:

首先你在 .h 文件中创建 IBOutlet

IBOutlet UIProgressView * threadProgressView;

然后在viewdidload中的.m文件中先设置progress为0.0然后调用makeMyProgressMoving方法

    threadProgressView.progress = 0.0;
    [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];

然后添加下面的方法

- (void)makeMyProgressBarMoving {

        float actual = [threadProgressView progress];
        if (actual < 1) {
            threadProgressView.progress = actual + ((float)recievedData/(float)xpectedTotalSize);
            [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
        }
        else{



        }

    } 

也给出您的评论以获得答案。对你有用吗?

【讨论】:

    【解决方案2】:

    这很简单。您只需要设置UIProgressView 的属性progress 的适当值。

    NSURLConnection 的委托中,您应该收到等待下载的数据量,并在下载过程中更新progressProgress 由一个介于 0.0 和 1.0(含)之间的浮点值表示,其中 1.0 表示任务完成。

    【讨论】:

      【解决方案3】:

      这行代码可以显示进度条的进度

      -(void) connection:(NSURLConnection *) connection 
      didReceiveData:(NSData *) data {
         if (file)
         { 
             [file seekToEndOfFile];
              progressView.progress = ((float)recievedData / (float) xpectedTotalSize);
         } 
           [file writeData:data];
           recievedData += data.length;
           NSLog(@"Receiving Bytes: %d", recievedData);
      }
      

      【讨论】:

        【解决方案4】:

        其中一个选项是 AFNetworking。 AFURLConnectionOperation 还允许您轻松地流式传输上传和下载、处理身份验证挑战、监控上传和下载进度以及控制缓存行为或请求。

        【讨论】:

          【解决方案5】:

          注意:self.progressionBalance.progress = 5.0/10.0;

          你必须设置小数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多