Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 用于本地复制文件的UiAlertView和UIProgressView_Ios_Uialertview_Uiprogressview - Fatal编程技术网

Ios 用于本地复制文件的UiAlertView和UIProgressView

Ios 用于本地复制文件的UiAlertView和UIProgressView,ios,uialertview,uiprogressview,Ios,Uialertview,Uiprogressview,我已经试着让它工作了一天了,但我还是失败了。安装应用程序时,我想将一些文件从捆绑包复制到我的应用程序的Documents文件夹中,但这会让用户在应用程序显示初始屏幕时等待很长时间 因此,我想我应该创建一个初始UIAlertView,其中UIProgressView作为子视图,每次文件复制到documents文件夹时都会更新。但是,警报会显示,进度条永远不会更新。我的逻辑是: 将UIProgressView和UIAlertView设置为我的ViewController的实例变量 在ViewDid

我已经试着让它工作了一天了,但我还是失败了。安装应用程序时,我想将一些文件从捆绑包复制到我的应用程序的Documents文件夹中,但这会让用户在应用程序显示初始屏幕时等待很长时间

因此,我想我应该创建一个初始UIAlertView,其中UIProgressView作为子视图,每次文件复制到documents文件夹时都会更新。但是,警报会显示,进度条永远不会更新。我的逻辑是:

  • 将UIProgressView和UIAlertView设置为我的ViewController的实例变量
  • 在ViewDidLoad中,显示警报并设置代理
  • -(void)didPresentAlertView:(UIAlertView*)alertView
    中执行for循环,复制文件并更新UI。代码是:

    - (void)didPresentAlertView:(UIAlertView *)alertView{
        NSString *src, *path;
        src = // path to the Bundle folder where the docs are stored //
        NSArray *docs = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:src error:nil];
    
        float total = (float)[docs count];
        float index = 1;
    
        for (NSString *filename in docs){
            path = [src stringByAppendingPathComponent:filename];
            if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
                ... // Copy files into documents folder
                [self performSelectorOnMainThread:@selector(changeUI:) withObject:[NSNumber numberWithFloat:index/total] waitUntilDone:YES];                
                index++;
            }
        }
    [alertView dismissWithClickedButtonIndex:-1 animated:YES];
    }
    
ChangeUI的代码是

- (void) changeUI: (NSNumber*)value{
    NSLog(@"change ui %f", value.floatValue);
    [progressBar setProgress:value.floatValue];
}
但是,这只是将UI从0更新为1,尽管NSLog会打印所有中间值。这里有人知道我做错了什么吗


提前感谢。

问题是您的循环位于主线程上,因此UI在最后才有机会更新。尝试使用GCD在后台线程上执行此工作:

dispatch_async(DISPATCH_QUEUE_PRIORITY_DEFAULT, ^
    {
        NSString *src, *path;
        src = // path to the Bundle folder where the docs are stored //
        NSArray *docs = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:src error:nil];

        float total = (float)[docs count];
        float index = 1;

        for (NSString *filename in docs){
            path = [src stringByAppendingPathComponent:filename];
            if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
                ... // Copy files into documents folder
                dispatch_async(dispatch_get_main_queue(), ^{ [self changeUI:[NSNumber numberWithFloat:index/total]]; } );

                index++;
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{ [alertView dismissWithClickedButtonIndex:-1 animated:YES]; } );
    } );

你试过调用“-(void)setProgress:(float)progress animated:(BOOL)animated”吗?是的,但是如果iOS<5.0,它会崩溃,这对我来说不好。这与调用
[自执行选择器背景:@selector(changeUI:)with object:[NSNumber numberWithFloat:index/total]
相同吗?不,不完全一样。请看Apple Dock中关于该方法的描述-您需要“正确设置”。未来的潮流是中央大调度和阻塞——苹果不鼓励NSThreads,他们一直在WWDC公开说这一点。上面的调度使用苹果已经正确设置的线程。GCD和ARC让苹果平台上的编程变得非常壮观!!!好的,谢谢@David。我已经接受了答案,现在我需要看一下文档。代码正常吗?我使用了它,并向需要非null参数的被调用方传递了一个警告null。当我运行它时,我得到了一个坏的访问。顺便说一句,performSelectorInBackground适合我。