這是我的第一個iPhone應用程序,我使用JSON框架來解碼從服務器發送的JSON。在兩個類之間共享數據
我將數據從AppDelegate文件插入到NSMutableArray中。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
responseData = [[NSMutableData data] retain];
museums = [[NSMutableArray alloc] init];
viewController = [[RootViewController alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://my_json_link"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *data = (NSDictionary *)[json objectWithString:responseString error:&error];
[responseString release];
if (data == nil)
NSLog(@"JSON parsing failed: %@", [error localizedDescription]);
else {
for (NSDictionary *item in data) {
// Read the data
NSString *aName = [item objectForKey:@"name"];
NSString *aDescription = [item objectForKey:@"description"];
// Create a new museum object with the data from json
Museum *museum = [[Museum alloc] initWithName:aName description:aDescription];
// Add the museum object to the Array
[museums addObject:museum];
[museum release];
}
}
viewController.museums = museums;
}
的博物館數組不connectionDidFinishLoading功能裏面空的,但我不能看到它時,我嘗試打印在RootViewController的。 我試圖設置陣列中的行
viewController.museums = museums;
,但我不明白什麼是錯的。從第一功能connectionDidFinishLoading功能
[window addSubview:viewController.view];
[window makeKeyAndVisible];
:
我可以解決這個問題只有當我移動這些線。但是在這種情況下,當我單擊表格的一個記錄時,其他視圖不起作用。
感謝您的任何幫助。
我已編輯您的文章標題以反映實際問題。隨意回滾,如果你認爲這是不恰當的:) – willcodejavaforfood 2010-09-13 17:15:20
而且我已經更新了我的答案,解決了我認爲你的問題是:) – willcodejavaforfood 2010-09-13 17:19:48