这是我的第一个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