2016-04-02 43 views
0

我有TableViewControllerViewController。我想在下載音頻後進入ViewController。但是我現在正在進行音頻下載。我使用fileExists它檢查是否下載音頻。但它不起作用。如何在ViewController下載音頻後進入?如何轉到ViewController?

這是我下載的代碼去

if (!fileExists) { 

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Download audio?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* actionAdd = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
     spinner.frame = CGRectMake(0, 0, 24, 24); 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     cell.accessoryView = spinner; 
     [spinner startAnimating]; 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39R2Z5RlVWZkN3Vzg"; 
       NSURL *url = [NSURL URLWithString:stringURL]; 
       NSData *urlData = [NSData dataWithContentsOfURL:url]; 
       [urlData writeToFile:filePath atomically:YES]; 

     }); 
     if (fileExists) { 
     [self performSegueWithIdentifier: @"detailSegue" sender: self]; 
      } 

    }]; 
    UIAlertAction* actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 

    }]; 

    [alert addAction:actionAdd]; 
    [alert addAction :actionCancel]; 

    [self presentViewController:alert animated:YES completion:nil]; 
     } 
     if (fileExists) { 
     [self performSegueWithIdentifier: @"detailSegue" sender: self]; 

     } 

    } 
+0

如果添加(FILEEXISTS){ [自performSegueWithIdentifier:@ 「detailSegue」 發件人:自我]。 }在你的dispatch_async代碼塊 –

+0

而不是'dataWithContentsOfURL'你應該使用NSURLSession'downloadTaskWithURL'或'dataTaskWithURL'這將給你委託方法或完成塊,你可以做下載完成後你必須做的編碼 – HardikDG

回答

0

// **嘗試這一個,並設置之前你標識符的ViewController在檢查**

ViewController *vc17 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; 

[self presentViewController:vc17 animated:YES completion:nil]; 
0

你應該總是在主隊列執行UI相關的代碼,如:

[self performSegueWithIdentifier: @"detailSegue" sender: self]; 

你最好使用NSURLSessionDataTask來從網上下載一些東西。

+0

爲什麼更好地使用NSURLSessionDataTask? – user

+0

因爲這是正式推薦的與互聯網溝通的方式,請閱讀:https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/#//apple_ref/occ/instm/NSURLSession/ downloadTaskWithResumeData – MudOnTire

相關問題