簡體   English   中英

NSURLConnection重新加載我的視圖

[英]NSURLConnection Reloading My View

因此,我正在使用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];
}

全部在您的代碼中:

    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; [HUD release]; HUD = nil;
  • 目前尚不清楚[window removeFromSuperview]試圖實現什么(它可能什么也不做,因為Windows沒有超級視圖)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM