2010-09-13 192 views
0

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

我可以解决这个问题只有当我移动这些线。但是在这种情况下,当我单击表格的一个记录时,其他视图不起作用。

感谢您的任何帮助。

+0

我已编辑您的文章标题以反映实际问题。随意回滚,如果你认为这是不恰当的:) – willcodejavaforfood 2010-09-13 17:15:20

+0

而且我已经更新了我的答案,解决了我认为你的问题是:) – willcodejavaforfood 2010-09-13 17:19:48

回答

1
viewController = [[RootViewController alloc] init]; 

首先,我需要你确保你在didFinishLaunching...创建viewController实际上是正确的viewController。你真的把那个实例连接成你认为的那样吗?还是你两个实例操作系统是RootViewController

其次,如果您实际设置的是RootViewController的正确实例上的博物馆,则需要确保您的时间安排正确。这意味着,你要为museums之前,你想打印出来的viewController

- 编辑 -

OK,因为我们建立的东西都是在错误的顺序,你应该尝试并重新加载表发生。 UITableView有一个名为reloadData的方法,将为您处理此问题,并且您需要在创建表后每次更改数据源时调用此方法。

所以在RootViewController的添加一个方法叫做重载这反过来对你的UITableView电话reloadData和修改代码:

viewController.museums = museums; 
[viewController reload]; 
+0

感谢您的重播。你是对的,如果我调试代码RootViewController在委托之前被调用,这就是为什么我不能在表中看到任何东西。对不起,但我是新的iPhone应用程序,我不知道如何连线两个实例。我需要在MainWindow.xib中完成吗? – Katie 2010-09-13 14:43:01

+0

所以它发生在错误的顺序? viewController是否有一个应该显示数据的表? – willcodejavaforfood 2010-09-13 17:11:01

-1

尝试使用

viewController.museums = [museums retain]; 
+0

是的,我有同样的问题 – Katie 2010-09-13 11:50:37

+1

不需要(将创建内存泄漏)if你的viewController的'博物馆'被宣布为'保留'属性,这是它被宣布的正常方式。 – 2010-09-13 13:51:18

+0

不一定。当你使用保留,你必须释放,并且不会有任何漏洞。 – Satyam 2010-09-14 09:23:23

0

尝试将NSLog的内部for循环,如果执行的时候检查。

1

你可以添加到您的视图控制器,暂时只进行调试:

-(void) setMuseums:(NSMutableArray*)m { 
    self->_museums = [m retain]; 
} 

然后在那里添加一个断点。确保它正在被击中,或者稍后有些事情会发生,并将其设置为零。

博物馆物业被宣布为@property(nonatomic,retain)NSMutableArray *博物馆;对?

+0

谢谢。是的,该物业是(非原子,保留)。现在我试图插入setMuseums,我可以看到博物馆数组已设置,但在numberOfRowsInSection函数之后。所以有什么不对,但我不明白在哪里。 – Katie 2010-09-13 15:06:56

+0

太棒了!然后,您可能只需在设置博物馆资产后重新加载其数据。设置博物馆后,请执行[viewController.tableView reloadData] – 2010-09-13 17:38:52