繁体   English   中英

在两个班级之间共享数据

[英]Sharing data between two classes

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

但是我不明白怎么了

只有移动这些行,我才能解决问题:

[window addSubview:viewController.view];  
[window makeKeyAndVisible]; 

从第一个函数到connectionDidFinishLoading函数。 但是在这种情况下,当我单击表的一条记录时,另一视图将不起作用。

谢谢你的帮助。

viewController = [[RootViewController alloc] init];

首先,我需要您确保在didFinishLaunching...创建的viewController实际上是正确的viewController 您是否实际上已将该实例连接为您认为的样子,还是您是两个实例RootViewController

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

- 编辑 -

好的,因为我们确定事情以错误的顺序发生,所以您应该尝试重新加载表。 UITableView有一个名为reloadData的方法,该方法将为您解决此问题,并且在创建表后每次更改数据源时都需要调用此方法。

因此,在RootViewController中添加一个名为reload的方法,该方法依次在UITableView上调用reloadData并修改代码:

viewController.museums = museums; 
[viewController reload];

您可以临时添加到视图控制器中,仅用于调试:

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

然后在其中添加一个断点。 确保它被击中,否则以后可能会出现一些东西并将其设置为零。

Museums属性声明为@property(非原子性,保留)NSMutableArray * museums; 对?

尝试将NSLog放入for循环中,并检查其是否已执行。

尝试使用

viewController.museums = [museums retain];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM