2010-10-08 89 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