2012-10-01 63 views
0

我遇到了新的iOS 6問題。以前我瞭解「viewDidUnload」。這是我的理解,這是現在貶值,我有一些問題,結束網絡活動指標。以下是我的代碼。在此先感謝您的幫助!viewDidUnload幫助 - iPhone應用程序

#import "MapViewController.h" 

@implementation MapViewController 

@synthesize webview, url, activityindicator, searchbar; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     // Custom initialization. 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    webview.delegate = self; 
    activityindicator.hidden = TRUE; 
    [webview performSelectorOnMainThread:@selector(loadRequest:) withObject:requestObj waitUntilDone:NO]; 
    [super viewDidLoad]; 
} 

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    activityindicator.hidden = TRUE; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [activityindicator stopAnimating]; 
    NSLog(@"Web View started loading..."); 
} 

- (void)webViewDidStartLoad:(UIWebView *)webView {  
    activityindicator.hidden = FALSE; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    [activityindicator startAnimating];  
    NSLog(@"Web View Did finish loading"); 
} 

- (void)didReceiveMemoryWarning { 
     // Releases the view if it doesn't have a superview. 
     [super didReceiveMemoryWarning]; 

     // Release any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    webview = nil; 
    activityindicator = nil; 
    searchbar = nil; 
    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [url release]; 
    [super dealloc]; 
} 

@end 
+0

你介意重新格式化你的代碼,以便像我這樣的老人可以閱讀和理解它嗎? – Till

+0

不應該在webViewDidFinishLoad中將networkActivityIndi​​catorVisible設置爲NO,並且在webViewDidStartLoad中將YES設置爲YES,而不是相反? – Sascha

回答

2

我想你誤解了viewDidUnload是爲了什麼。您的代碼與隱藏「viewDidUnload」中的活動微調器無關。

- (void)viewDidUnload 
{ 
    webview = nil; 
    activityindicator = nil; 
    searchbar = nil; 
    [super viewDidUnload]; 
} 

viewDidUnload是永遠只能意味着清理保留下來,更換對象時,系統內存不足的情況下,在清除了你的UIViewController的非活動視圖。

在iOS 6中,viewDidUnload永遠不會被調用,因爲系統將不再在低內存情況下清除UIViewController的視圖,如果您在didReceiveMemoryWarning回調中需要,也可以這樣做。

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    if ([self isViewLoaded] && self.view.window == nil) 
    { 
    self.view = nil; 
    [self viewDidUnload]; 
    } 
} 
+0

+1在指出viewDidUnload的目的和爲iOS6顯示一個合適的替代方案方面做得不錯 - 同樣在重新格式化這個混亂:D – Till

+0

@歡呼聲。格式化代碼可以是治療性的;) – Jessedc

+0

感謝您幫助我理解viewDidUnlaod的真實用途。我會用新的視角來看看我的代碼。謝謝 – user1713012