0

我在这里遇到了一些问题,我让用户将一些视频上传到服务器,但是我在管理用于说明其进度的视图时遇到了一些困难,我知道为什么会出现问题并且我发现了绕过它(有点)我的问题

因此,如果有人尝试编写一些看起来像这样的代码(在 UIViewController

-(void)uploadMovie
{
   UIActivityView indicator=new...
  [self.view addSubview:indicator]
   [uploader UploadMyMovie:data]

 }

此代码不起作用,上传器将锁定控制器并且不允许指示器及时出现在屏幕上,我发现在调用上传器之前等待几秒钟,但我采取了另一种方法。

该方法是为上传者启动一个新线程,并有一个协议,其中上传者对象在开始上传时通知 UIViewController(或某个委托),它的进度以及何时完成上传。就像是

     -(void)uploadMovie
    {
       UIActivityView indicator=new...
      [self.view addSubview:indicator]
       NSThread *thread=...
       [thread start]
     }

委托方法看起来像这样

    #pragma mark UploadProgressDelegate

-(void)didStartUploading
{

    progressLabel= [[UILabel alloc] initWithFrame:CGRectMake(93, 240, 116, 32)];
    ind= [[UIActivityIndicatorView alloc]   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    ind.center=self.view.center;
    [progressLabel setTextColor:[UIColor blackColor]];
    [progressLabel setText:@"TEST"];
    [self.view addSubview:progressLabel];
    [self.view addSubview:ind];
    [ind startAnimating];
    [self.view bringSubviewToFront:progressLabel];
    [ind setHidesWhenStopped:TRUE]; 
    [self.view setUserInteractionEnabled:FALSE];
}
-(void)progressUpdate:(NSString*)progress
{
     [progressLabel setText:progress];
}
-(void)didEndUploading;
{
    [progressLabel removeFromSuperview];
    [ind stopAnimating];
    [ind removeFromSuperview];
    [progressLabel release];
    [ind release];
    [self.view setUserInteractionEnabled:TRUE];
}

这很好用,指示器显示和所有内容,然后我决定通过添加 UILabel 让用户看到进度(反映在上面的代码中),但是为此解决方案不起作用,标签不显示,当然也没有更新...

我想知道是否有人遇到过这种情况并提出了解决方案?或者如果您可以从上面的代码中看到为什么标签没有显示......

谢谢

4

2 回答 2

1

在某些情况下,我发现我需要回到主线程来做某些事情......

所以在你的委托方法中你会做

[self performSelectorOnMainThread: @selector(updateLabel:) withObject:newLabelText waitUntilDone:NO];

进而

- (void) updateLabel:(NSString *)newLabelText
{
    [progressLabel setText:newLabelText];
}

不过,我不确定必须在主线程而不是在后台完成哪些事情的规则。

于 2009-07-25T03:33:04.037 回答
1

UIKit 不是线程安全的。如果您正在更新 UI 元素,则需要同步回主线程,否则所有赌注都将关闭。

于 2009-07-25T04:53:55.527 回答