所以我使用NSURLConnection委托在我的AppDelegate.m文件中下载JSON提要。现在我有一个问题。我的应用程序:didFinishLaunchingWithOptions如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Display the progress indicator
HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];
// Start the HTTP request
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"*****some url here*********"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Display the navigation controller
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
所以我所做的就是显示一个活动指示器,启动HTTP请求(这是NSURLConnection委托开始工作的时候),然后我显示我的导航控制器的根视图控制器。现在问题是它将加载具有表视图的视图,其中没有任何数据(因为NSURLConnection)尚未完成下载数据。它看起来很丑陋,因为我在界面构建器上创建了静态UILabel,因此它显示它们然后在加载后它将显示数据。我在屏幕中间有一个活动指示器,因此用户始终知道正在加载新数据。
现在,在我的NSURLConnection完成下载数据之后,由于一些奇怪的原因,视图再次加载了一次。我知道因为如果我把NSLog(@“Test”);在我的viewDidLoad方法中,它打印出两次。在应用程序结束时:didFinishLaunchingWithOptions并在connectionDidFinishLoading结束时。这是为什么?如何使其看起来整洁,在我的数据加载后视图加载一次?有什么建议吗?
谢谢大家,
编辑:申请前AppDelegate中的更多代码:didFinishLaunchingWithOptions
- (void)hudWasHidden {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
[HUD release];
}
#pragma mark -
#pragma mark NSURLConnectionDelegete
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Starting Response");
[responseData setLength:0];
expectedLength = [response expectedContentLength];
currentLength = 0;
// Change the progress indicator to the loading wheel
HUD.mode = MBProgressHUDModeDeterminate;
NSLog(@"Done Response");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Starting Data");
[responseData appendData:data];
currentLength += [data length];
// Calculate progress indicator progress
HUD.progress = currentLength / (float)expectedLength;
NSLog(@"Done Data");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Start Error");
[HUD hide:YES];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Please check your network connection and relaunch the application"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[connection release];
NSLog(@"Done Error");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Start Finish Loading");
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if ([responseString isEqualToString:@"Unable to find specified resource."]) {
NSLog(@"Unable to find specified resource.n");
}
else {
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:@"ListingsViewController" bundle:nil];
listingsViewController.jsonData = responseString;
[self.navigationController pushViewController:listingsViewController animated:NO];
[self.navigationController setViewControllers:[NSArray arrayWithObject:listingsViewController] animated:NO];
[listingsViewController release];
}
// Save the pList to the Documents directory in iOS
ListingsViewController *listingsViewController = (ListingsViewController *)[self.navigationController topViewController];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *plistFilePathInDocumentsDirectory = [documentsDirectoryPath stringByAppendingPathComponent:@"Channels.plist"];
// Instantiate a modifiable array and initialize it with the content of the plist file
NSMutableDictionary *channelsData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilePathInDocumentsDirectory];
if (!channelsData) {
NSString *plistFilePathInMainBundle = [[NSBundle mainBundle] pathForResource:@"Channels" ofType:@"plist"];
// Instantiate a modifiable array and initialize it with the content of the plist file in main bundle
channelsData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilePathInMainBundle];
}
// Set the ListingsViewController's's savedChannels array
listingsViewController.channelsDict = channelsData;
[channelsData release];
// Display progress indicator checkmark and hide
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:1];
[connection release];
[responseData release];
NSLog(@"Done finish loading");
[window removeFromSuperview];
}
答案 0 :(得分:0)
这些都在你的代码中:
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:@"ListingsViewController" bundle:nil];
listingsViewController.jsonData = responseString;
[self.navigationController pushViewController:listingsViewController animated:NO];
[self.navigationController setViewControllers:[NSArray arrayWithObject:listingsViewController] animated:NO];
[listingsViewController release];
您正在创建一个新的ListingsViewController并用它替换旧的。新的ListingsViewController尚未加载其视图。 (您不需要同时调用-pushViewController:animated:
和-setViewControllers:
;在这种情况下,第二个会覆盖第一个。)
其他事项:
-hudWasHidden
中,您可能希望[HUD release]; HUD = nil;
。[window removeFromSuperview]
试图实现什么(它可能什么也没做,因为Windows没有超视图)。