2

我有一個TableViewController帶有一個按鈕,觸發一個[NSURLConnection sendAsynchronousRequest...]事件,並且還通過performSegueWithIdentifier加載模式segue:sender:它定位一個小視圖控制器。此覆蓋視圖控制器的目的是顯示加載圖形並防止用戶在數據通過NSURLConnection發送時的交互。以編程方式解除視圖控制器時的警告

NSURLConnection的完成塊中,我調用刪除TableViewController(它只是一個批次列表)中的數據的方法,然後在覆蓋視圖控制器上調用dismissViewControllerAnimated:completion:

除了關閉覆蓋視圖控制器,它會在調試器中引發一個警告,該警告說: 「警告:試圖在演示或解散過程中關閉視圖控制器!

我發現了關於這個錯誤的各種問題和答案,特別是關於使用performSelector:object:withDelay方法,但到目前爲止沒有任何工作。

這是特別煩,因爲我使用類似的過程中應用的其他區域,除了dismissViewController從選擇UITableViewCell調用,這工作得很好...

我的代碼的相關位如下圖所示:

#import "ViewBatch.h" 

@interface ViewBatch() 
@property (strong, nonatomic) LoadingOverlayViewController *loadingOverlay; 
@end 

@implementation ViewBatch 
@synthesize loadingOverlay; 

.... 

- (IBAction)exportBatch:(id)sender 
{ 
    if ([productArray count] > 0) { 
     [self performSegueWithIdentifier:@"loadingSegue" sender:self]; 
     [self processData]; 
    } 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"loadingSegue"]) { 

     loadingOverlay = segue.destinationViewController; 
    } 
} 

- (void)processData 
{ 

    // Code to create a file and NSURLRequest... 
    // .... 

    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:urlRequest 
             queue:queue 
          completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { 
           if ([responseData length] > 0 && error == nil) 
           { 
            // Not used for this request yet... 
           } 
           else if ([responseData length] == 0 && error == nil) 
           { 
            // Success... 
            [self didSendData]; 
           } 
           else if (error != nil) 
           { 
            // Connection error... 
            NSLog(@"Error: %@", error); 
           } 
          }]; 
} 

- (void)didSendData 
{ 
    // Reset the batch... 

    [productArray removeAllObjects]; 
    [self.tableView reloadData]; 

    [loadingOverlay dismissViewControllerAnimated:YES completion:NULL]; 
} 

,裝載視圖控制器就是:

#import <UIKit/UIKit.h> 

@interface LoadingOverlayViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UILabel *statusLabel; 
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; 

@end 

.... 

.... 

#import "LoadingOverlayViewController.h" 

@interface LoadingOverlayViewController() 
@end 

@implementation LoadingOverlayViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.activityIndicator startAnimating]; 
} 

@end 

回答

1

我不確定問題的確切原因,但下面是關於您的代碼的一些觀察。

  • 爲了防止用戶交互,您不需要覆蓋。只需關閉與-NSApplication beginIgnoringInteractionEvents的用戶交互。

  • 您不需要整個視圖控制器來顯示重疊視圖。只顯示視圖。例如,當NSURLConnection正在發生時,我經常在視圖的中間放置一個大的UIActivityIndi​​catorView。

  • 您不需要NSOperationQueue以便異步使用NSURLConnection。它已經是異步的。只需創建NSURLConnection並等待委託消息到達。實際上,由於您設置了輔助隊列,消息到達該隊列,並且您撥打didSendData,在表格上調用reloadData - 在後臺,這是非法的。如果以正常方式執行此操作,則代理消息將到達主線程,而這正是您想要的。

+0

謝謝亞光。我確實得到它的工作,但我會嘗試重新編碼,如你所建議的。 – beltanegrey

0

沒關係,我得到了它的工作。 驚人的半小時休息可以做組織一個人的想法...

該過程完成並在實際視圖甚至完成加載之前調用dismissViewController。來自覆蓋層上viewDidLoad的簡單委託調用將事件整理出來。

0

我也有這個問題。我注意到這是複製粘貼我的筆尖文件中的按鈕的結果。這樣做的結果是我不得不IBActions綁兩個1 UIButton。

相關問題