UIAlertView 与 UIActivityIndi​​cator 有点矛盾

Posted

技术标签:

【中文标题】UIAlertView 与 UIActivityIndi​​cator 有点矛盾【英文标题】:UIAlertView kind of contradicts the UIActivityIndicator 【发布时间】:2012-03-20 14:56:59 【问题描述】:

我正在从在线图像下载图像以在离线时进行缓存。当我打开应用程序时,图像在 UIAlertView 弹出之前已完全下载。它完成不正确。我想让 UIAlertView 在图像下载之前弹出。下面是我的代码。

- (void) operatePopupUpdating 
    myAlert = [[UIAlertView alloc] initWithTitle:@"Updating database.." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [myAlert show];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator.center = CGPointMake(myAlert.bounds.size.width / 2, myAlert.bounds.size.height - 50);
    [indicator startAnimating];
    [myAlert addSubview:indicator];

    [self operateUpdate];
    [self updateImage];

    [self operationCompleted];

在updateImage中,operateUpdate和operationCompleted方法

- (void)updateImage 
NSString *snake_ico_file = @"snake_img_icn_";
NSString *filePath = [self dataFile:[snake_ico_file stringByAppendingString:@"0.jpg"]];
int max_count = [[self readData] count];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    for (int i = 0; i < max_count; i++) 
        NSString *path = @"http://exitosus.no.de/drngoo/image/";
        path = [path stringByAppendingFormat:@"%d",i];
        NSString *ico_path = [path stringByAppendingFormat:@"/ico"];

        //icon            
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filename = [snake_ico_file stringByAppendingFormat:@"%d.jpg"];
        NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:filename];
        NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ico_path]];;
        [thedata writeToFile:localFilePath atomically:YES];
       


-(void) operationCompleted

       [myAlert dismissWithClickedButtonIndex:0 animated:YES];

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message form System" message:@"Database was updated successful" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

       [alert show];


- (void)operateUpdate 

     NSString *filePath = [self dataFileSettingPath];
     int updatedVer = 0;
     int version = [self checkDatabase];
     if (version == -1) 
         updatedVer = [self createDataBase:0];
      else 
         updatedVer = [self updateDataBase:version];
     

     [self.settingInfo setObject:[NSString stringWithFormat:@"%d",updatedVer] forKey:@"DBVersion"];
   [self.settingInfo writeToFile:filePath atomically:YES];

我如何修复它们并正常工作?

【问题讨论】:

请编辑您的标题或问题的内容:UIAlertView 有点与 UIActivityIndi​​cator 相矛盾。 它检查数据库以进行更新。我在上面编辑了我的代码。 【参考方案1】:

alertView 仅在您的图片下载完成后才会显示。解决此问题的一种方法是,

创建另一个类 ImageDownLoad.h

在.h中

@property(nonatomic,assign)id delegate;
@property(nonatomic,retain) NSString *path;

在.m

@synthesize delegate;
@synthesize path;

即创建一个方法,

-(void)startDownloadImageWithUrl:(NSURL*)imageUrl withLocalFilePath:(NSSTring*)filePath
       path = filePath;
       receiveData = [NSMutableData data];
       NSURLRequest *request = [[NSURLRequest alloc]initWithURL:imageUrl];
       NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request];
 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
        [receiveData appendData:data];


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
        [(id)delegate performSelector:@selector(SaveData:inFilePath:) withObject:receiveData withObject:path];

这将创建连接并开始在单独的类中下载您的图像。然后在你的 mai ViewController 中添加如下代码。

  - (void)updateImage 
          NSString *snake_ico_file = @"snake_img_icn_";
          NSString *filePath = [self dataFile:[snake_ico_file stringByAppendingString:@"0.jpg"]];
          int max_count = [[self readData] count];
          if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
                 for (int i = 0; i < max_count; i++) 
                 NSString *path = @"http://exitosus.no.de/drngoo/image/";
                 path = [path stringByAppendingFormat:@"%d",i];
                 NSString *ico_path = [path stringByAppendingFormat:@"/ico"];

                      //icon            
                 NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                 NSString *documentsDirectory = [paths objectAtIndex:0];
                 NSString *filename = [snake_ico_file stringByAppendingFormat:@"%d.jpg"];
                 NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:filename];

                 ImageDownLoad *imageDown = [[ImageDownLoad alloc]init];
                 imageDown.delegate = self;
                 [imageDown startDownloadImageWithUrl:[NSURL URLWithString:ico_path] withLocalFilePath:localFilePath];

            
   

每次图片下载完成时都会调用该方法。

   -(void)SaveData:(NSData*)data inFilePath:(NSString*)filePath

          [data writeToFile:filePath atomically:YES];

   

您的 UI 不会因此而延迟 :)

【讨论】:

我用这个建议实现,然后我得到 SaveData]: unrecognized selector sent to instance。 这种方法可以等待下载完成后再弹出消息吗? @WaiToNZa 我编辑过,应该类似于 [(id)delegate performSelector:@selector(SaveData:inFilePath:) withObject:receiveData withObject:path]; 非常感谢您解决了这个问题。 :D

以上是关于UIAlertView 与 UIActivityIndi​​cator 有点矛盾的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertView 与 UIActivityIndi​​cator 有点矛盾

UIAlertView 已弃用:首先在 iOS 9.0 中弃用 - UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用

UIAlertView 显示方法与新的 UIAlertController 对象等效吗?

提示窗UIAlertView与UIAlertController的用法(持续更新中)

iOS UIAlertview 与 uitextview 可编辑

UIAlertView 没有禁用所有交互