我有一個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
謝謝亞光。我確實得到它的工作,但我會嘗試重新編碼,如你所建議的。 – beltanegrey