ios - 数据同时显示在两个屏幕上

标签 ios objective-c

在我的应用中有一个侧边菜单和主屏幕,

enter image description here

数据来自网络服务,它返回 JSON 对象,

在JSON对象类别如1,2,3,4....和它的相关描述如图像和价格等来了。

它首先调用侧边菜单类的 ViewDidLoad,然后调用主屏幕类,所以我使用 NSURLConnection 对象在侧边菜单类中调用我的 Web 服务。

- (void)viewDidLoad
{

NSString *url_str=[NSString stringWithFormat:@"myurl.php"];
    NSURL *url=[NSURL URLWithString:url_str];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

    NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [response_data appendData:data];
}

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [response_data setLength:0];
}

-(void)connection :(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR WITH CONNECTION");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *myError;
    table_data=[NSJSONSerialization JSONObjectWithData:response_data options:NSJSONReadingMutableLeaves error:&myError];
    NSLog(@"JSON data  %@",table_data);
    [self.tableView reloadData];
}

响应数据正常但在 home ViewDidLoad 方法之后。所以我无法获取主屏幕的数据。

任何人都可以帮助我在获得网络服务响应后如何在两个屏幕上同时显示数据。

最佳答案

如果您有多个地方希望 UI 在操作完成时做出响应(即通过显示加载数据的结果),您可以使用 NSNotificationCenter 来广播通知。

在您的connectionDidFinishLoading方法中,您需要发送如下通知:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *myError;
    table_data=[NSJSONSerialization JSONObjectWithData:response_data options:NSJSONReadingMutableLeaves error:&myError];
    NSLog(@"JSON data  %@",table_data);
    [self.tableView reloadData];

    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:table_data forKey:@"table_data"];
    [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"DataLoadedNotification" 
        object:nil userInfo:userInfo];

}

在主屏幕中,您可以通过在 viewDidLoad 方法中执行以下操作来监听通知并在屏幕上重新绘制数据:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receiveDataLoadNotification:) 
    name:@"DataLoadedNotification"
    object:nil];

当然,您需要实现收到通知时调用的方法:

- (void) receiveDataLoadNotification:(NSNotification *) notification
{
    // [notification name] should always be @"DataLoadedNotification"
    // unless you use this method for observation of other notifications
    // as well.

    if ([[notification name] isEqualToString:@"DataLoadedNotification"])
    {
        NSDictionary *userInfo = notification.userInfo;
        JSONObject *table_data = [userInfo objectForKey:@"table_data"];
        NSLog (@"Successfully received the data loaded notification!");
    }
}

如果需要,可以拉取table_data对象

关于ios - 数据同时显示在两个屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25217776/

相关文章:

ios - for 循环中的强类对象未保留在 ARC 中

ios - 加载更多到 UITableView 中的最后一个位置

iphone - 如何使用NSJSONSerialization类将数据发布到基于JSON的REST API?

ios - CCBlade 不适用于 cocos2d-x 3.8 版本

ios - 将 Char 或 NSString 添加到 Objective C 枚举中

ios - 如何同时滑动标签文本和图像

ios - UITableViewController 中的 UITableView

ios - OAuth 2 与 IOS、node.js、passport 和 google

ios - 如何将 UIButton 的标题设置为左对齐?

javascript - Xcode 中的 XML Dom 树等效项