2010-10-08 65 views
1

這可能是一個愚蠢的問題,並帶有簡單的答案。我是iOS開發新手,這讓我很難過。iphone標籤欄應用程序視圖生命週期問題

我有一個基本的標籤欄應用程序。該應用程序在啓動時加載初始選項卡,並且它是一個tableview控制器。這個tableview的數據是由一個datacontroller提供的,它通過從互聯網獲取的數據創建一個數組。當我第一次啓動應用程序時,沒有任何東西顯示在桌面視圖中,它是空的。但是,如果我選擇不同的選項卡,然後返回到該選項卡,數據現在就會顯示在桌面視圖中,就像它應該那樣。我如何獲得第一次顯示的數據?這顯然是在那裏。我相信這可能與數據加載導致問題的位置/時間有關,任何洞察力都會很好。

這臺DataController類的應用程序委託:

// Create Locations data controller. 
LocationDataController *controller = [[LocationDataController alloc] init]; 
self.locationDataController = controller; 
[controller release]; 
locationsViewController.locationDataController = locationDataController; 

這裏的DataController類代碼:

@implementation LocationDataController 
@synthesize fetchConnection = mFetchConnection; 
@synthesize locations, tempLocations; 



-(id)initData { 
    if (self = [super init]) { 
     [self createData]; 
    } 
    return self; 
} 

-(id)init { 
    [self fetchData]; 
    return self; 
} 


-(unsigned)countOfList { 
    return [locations count]; 
} 


-(Location *)objectInListAtIndex:(unsigned)theIndex { 
    return [locations objectAtIndex:theIndex]; 
} 


-(void)dealloc { 
    [locations release]; 

    if (mFetchConnection != nil) { 
     [mFetchConnection cancel]; 
     [mFetchConnection release]; 
    } 

    if (mFetchConnectionBuffer != nil) 
     [mFetchConnectionBuffer release]; 

    [super dealloc]; 
} 


#pragma mark - 
#pragma mark Fetching/Sending Data 
#pragma mark 

- (void)fetchData { 
    if (mFetchConnection == nil) { 
     if (mFetchConnectionBuffer != nil) 
      [mFetchConnection release]; 

     mFetchConnectionBuffer = [[NSMutableData alloc] init]; 

     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:kFetchURLPath]]; 
     self.fetchConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease]; 
     [request release]; 
    } 
} 




#pragma mark - 
#pragma mark NSURLConnection Delegate 
#pragma mark 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)inData { 
    if (connection == mFetchConnection) { 
     [mFetchConnectionBuffer appendData:inData]; 
    } 
} 




- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)connectionError { 
    if (connection == mFetchConnection) { 
     self.fetchConnection = nil; 
     [mFetchConnectionBuffer release]; 
     mFetchConnectionBuffer = nil; 
    } 
} 



- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    tempLocations = [[NSMutableArray alloc] init]; 

    if (connection == mFetchConnection) { 
     NSString *str = [[NSString alloc] initWithData:mFetchConnectionBuffer encoding:NSUTF8StringEncoding]; 
     SBJSON *parser = [[SBJSON alloc] init]; 
     id result = [parser objectWithString:str error:nil]; 
     if (result != nil && [result isKindOfClass:[NSArray class]]) 
      [tempLocations setArray:result]; 
     [str release]; 
     [mFetchConnectionBuffer release]; 
     mFetchConnectionBuffer = nil; 
     self.fetchConnection = nil; 

     [self initData]; 

    } 
} 


-(void)createData { 

    // Cycle through array to implement locations. 
    Location *location; 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 

    // create arry of locations. 
    for (int i=0; i<[tempLocations count];i++) { 

     location = [[Location alloc] init]; 
     location.name = [[tempLocations objectAtIndex:i] objectForKey:@"name"]; 
     location.address = [[tempLocations objectAtIndex:i] objectForKey:@"address"]; 
     location.city = [[tempLocations objectAtIndex:i] objectForKey:@"city"]; 
     location.state = [[tempLocations objectAtIndex:i] objectForKey:@"state"]; 
     location.zip = [[tempLocations objectAtIndex:i] objectForKey:@"zip"]; 

     [array addObject:location]; 
     [location release]; 

    } 

    self.locations = array; 
    [array release]; 
    [tempLocations release]; 

} 
+0

何時加載數據?在viewDidLoad中,viewDidAppear,其他?顯示一些執行數據加載的代碼可能會有所幫助。 – Anna 2010-10-08 17:23:19

+0

確實沒有在數據的tableview控制器中的任何代碼。 viewDidLoad或viewDidAppear中沒有任何內容。我綜合了locationDataController並使用它將信息拉入表格單元格。因爲我知道數據在那裏,所以我開始懷疑它是否是因爲它從互聯網上提取數據,並且在顯示tableview控制器之前可能沒有完成它的下載和數據設置? – user469626 2010-10-08 21:40:16

回答

0

假設你已經正確實施的TableView委託方法,我建議你打電話的tableView。在創建數據之後重新加載數據以查看是否有效。

+0

我試過了,但似乎沒有運氣。 - [self.tableView reloadData]這是在viewWillApear。也嘗試在viewDidLoad沒有運氣。 – user469626 2010-10-08 21:54:07

+0

您正在以異步方式加載數據,因此當您調用它時數據可能沒有準備好。只是一個猜測。 – Max 2010-10-08 22:14:46

+0

這是我剛開始討論的結論,關於我能做些什麼來修改它的想法/想法。我對如何解決這個問題感到不知所措。它可以工作,但似乎是在數據準備好之前它正在顯示視圖,我不知道如何解決問題的方案。 – user469626 2010-10-08 22:18:28