【发布时间】:2012-11-14 04:46:52
【问题描述】:
我有一个TableViewController,带有一个触发[NSURLConnection sendAsynchronousRequest...] 事件的按钮,并且还通过performSegueWithIdentifier:sender: 加载了一个模态segue,它针对一个小视图控制器。此覆盖视图控制器的目的是显示加载图形并防止用户在通过NSURLConnection 发送数据时进行交互。
在NSURLConnection 的完成块中,我调用一个方法来删除TableViewController 中的数据(它只是一个批处理列表),然后在覆盖视图控制器上调用dismissViewControllerAnimated:completion:。
除了关闭覆盖视图控制器外,一切正常,它会在调试器中引发警告,上面写着: “警告:在演示或关闭过程中尝试从视图控制器中关闭!”
我找到了有关此错误的各种问题和答案,尤其是有关使用 performSelector:object:withDelay 方法的问题,但到目前为止没有任何效果。
这特别烦人,因为我在应用程序的另一个区域使用了类似的过程,除了通过选择 UITableViewCell 调用dismissViewController,这工作正常...
我的代码的相关位如下所示:
#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
【问题讨论】:
标签: ios uiviewcontroller xcode4.5 objective-c-2.0 uistoryboardsegue