2013-11-26 20 views
0

我有一個視圖控制器,加載一些數組。當一切正在加載時,我需要提供另一個視圖控制器(使用UIProgressView)並更新它的UI(UIProgressView的progress屬性),然後解散並顯示下載的數據。我真的很努力,我已經嘗試過代表團,但沒有爲我工作。更新視圖控制器的用戶界面,然後解僱它

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"downloaded"]) { 

     } else { 
      NSLog(@"First time Launched"); 
      ProgressIndicatorViewController *progressVC = [ProgressIndicatorViewController new]; 
      progressVC.modalPresentationStyle = UIModalPresentationFullScreen; 
      [self syncContacts]; 
      [self presentViewController:progressVC animated:YES completion:nil]; 
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"downloaded"]; 
      [progressVC release]; 
     } 
    } 

同步觸點方法:

- (void)syncContacts 
{ 
    NSLog(@"Sync data"); 
    NSMutableArray *allContacts = [ContactsOperations getAllContactsFromAddressBook]; 
    NSInteger allContactsCount = [allContacts count]; 
    if (allContactsCount > 0) { 
     for (ContactData *contact in allContacts) { 
      NSMutableArray *phoneNumbersArray = [[NSMutableArray alloc] init]; 
      NSString *nospacestring = nil; 

      for (UserTelephone *tel in [contact.abonNumbers retain]) { 
       NSArray *words = [tel.phoneNumber componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]]; 
       NSString *nospacestring = [words componentsJoinedByString:@""]; 
       [phoneNumbersArray addObject:nospacestring]; 
      } 

      contact.abonNumbers = phoneNumbersArray; 

      if (phoneNumbersArray != nil) { 
       NSLog(@"NOT NULL PHONENUMBERS: %@", phoneNumbersArray); 
      } 

      NSDictionary *dataDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:contact.abonNumbers, @"phoneNumbers", contact.contactName, @"fullName", [NSNumber numberWithBool:contact.isBlackList], @"blacklist", [NSNumber numberWithBool:contact.isIgnore], @"ignore", contact.status, @"status", nil]; 
      NSLog(@"dictionary: %@", dataDictionary); 
      NSError *error; 
      NSData *postData = [NSJSONSerialization dataWithJSONObject:dataDictionary options:0 error:&error]; 
      NSLog(@"POST DATA IS : %@", postData); 

      NSMutableURLRequest *newRequest = [self generateRequest:[[NSString stringWithFormat:@"%@c/contacts%@%@", AVATATOR_ADDR, SESSION_PART, [[ServiceWorker sharedInstance] SessionID]] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] withHTTPMethod:@"POST"]; 
      [newRequest setHTTPBody:postData]; 
      [newRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
      //__block NSMutableData *newData; 
      [NSURLConnection sendAsynchronousRequest:newRequest queue:[NSOperationQueue mainQueue] 
            completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) 
      { 
       if (!connectionError) { 
        NSDictionary *allData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
        NSLog(@"alldata from contacts: %@", allData); 
        //NSInteger errorCode = [[allData objectForKey:@"CommandRes"] integerValue]; 
        //if (errorCode == 0) { 
         NSInteger remoteId = [[allData objectForKey:@"contactId"] integerValue]; 
         contact.remoteId = remoteId; 
         NSLog(@"remote id is from parse content : %d", remoteId); 
         [[AvatatorDBManager getSharedDBManager]createContactWithContactData:contact]; 
       } else { 
        NSLog(@"error"); 
       } 
      }]; 
      //Somewhere here I need to update the UI in another VC 
      [phoneNumbersArray release]; 
      [dataDictionary release]; 
     } 
    } else { 
    } 
} 

生成請求方法:

- (NSMutableURLRequest *)generateRequest:(NSString *)urlString withHTTPMethod:(NSString *)httpMethod 
{ 
    NSLog(@"url is :%@", urlString); 
    NSURL *url = [NSURL URLWithString:urlString]; 
    request = [NSMutableURLRequest requestWithURL:url]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [request setHTTPMethod:httpMethod]; 
    return request; 
} 

ProgressViewController只是一個空VC與進度條。還沒有代碼。

+0

您的代碼在哪裏? – Raptor

+1

那麼你的問題是什麼?你想從代碼中得到什麼?現在產生了什麼?有希望地用截圖指定。 – Bourne

+0

需要在單獨的視圖控制器中顯示進度條,還是需要以最有效的方式顯示進度欄?你是否簡單地假設它必須顯示在一個單獨的視圖控制器中? –

回答

1

在視圖控制器會顯示進度視圖暴露這樣的方法......

- (void)updateProgress:(float)progress; 

它的實施將看起來像這樣...

- (void)updateProgress:(float)progress { 
    [self.progressView setProgress:progress animated:YES]; 
} 

在主視圖控制器您需要在後臺線程上執行長時間運行的進程。這裏是主視圖控制器的viewDidLoad。此示例代碼使用進度視圖控制器的屬性(您可能不需要此操作),並假定您在導航控制器中...

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    // Create and push the progress view controller... 
    self.pvc = [[ProgressViewController alloc] init]; 
    [self.navigationController pushViewController:self.pvc animated:YES]; 

    // Your long-running process executes on a background thread... 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    // Your long-running process goes here. Wherever required you would 
    // call updateProgress but that needs to happen on the main queue... 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.pvc updateProgress:progress]; 
     }); 

     // At the end pop the progress view controller... 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.navigationController popViewControllerAnimated:YES]; 
     }); 
    }); 
} 
+0

感謝您的建議,但這是我第一次嘗試:)。問題是來自第一個vc的任務停止了 - 所以沒有更新。我通過在ProgressVC中運行這個任務解決了這個問題,但是非常感謝您的回答,先生。 – PAcan

+0

@PAcan我認爲這可能是您需要在後臺隊列上執行長時間運行的任務。我將編輯我的答案以表明這一點。 –

相關問題