如何使用从 ViewController 2 传回的数据更新 ViewController 1 数据

How do I update ViewController1 data with data passed back from ViewController2

我有一个加载 table 视图的导航VC。当您点击一个单元格时,它会将一些数据复制到详细信息VC,有一个更新按钮可以更新这些值。如何在不返回 table 视图并先在那里更新的情况下刷新详细信息 VC 中的数据?

这就是我想要做的:

TableView>DetailsView>EditDetails> Back to DetailsView with updated info

不是这个:

TableView>DetailsView>EditDetails>DetailsView(via navigation back)> TableView>(pull to refresh)>DetailsView

编辑

- (void) receiveNotifications:(NSNotification *) notification {
    if ([[notification name] isEqualToString:@"updateDetailsVC"]){
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            NSDictionary *userInfo = [notification userInfo];
            NSLog (@"%@", userInfo);

            PRIORITYstring = [userInfo objectForKey:@"PRIORITY"];
        });
    }
}

我能够让代码工作,并且通过 NSLog,我的数据被作为字典推送回来。

当 viewDidLoad for detailsVC 时,它运行一个将 NSString 放入其对应的 UILabel 的方法。

- (void)viewDidLoad {
    [super viewDidLoad];
    [self displayData];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(receiveNotifications:)
                                           name:@"updateDetailsVC"
                                           object:nil];
}



-(void)displayData {
        PRIORITYlabel.text = [NSString stringWithFormat:@"Short Description: \n%@", QDESCRIPTIONstring];
}


- (void) receiveNotifications:(NSNotification *) notification {
    if ([[notification name] isEqualToString:@"updateDetailsVC"]){
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            NSDictionary *userInfo = [notification userInfo];
            NSLog (@"%@", userInfo);

            PRIORITYstring = [userInfo objectForKey:@"PRIORITY"];
            //PRIORITYlabel.text = [userInfo objectForKey:@"PRIORITY"];
        });
    }
}

我该如何获取字典来覆盖初始数据?

你可以利用NSNotificationCenter。 Post 来自编辑视图的通知,并订阅来自 table 视图和详细信息视图的相同通知。

Post正在通知::

[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:nil userInfo: userInfo];

如果您想从编辑视图传递任何数据,可以使用 userInfo 字典!!

正在从 table 视图和详细视图订阅通知 ::

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

操作方法::

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

    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
        //retrieve the userInfo dictionary received from edit view
        NSDictionary *userInfo = [notification userInfo];
}

您可以在 table 视图和详细视图中添加此操作方法。

正在删除 table 视图和详细视图中的观察者 ::

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

编辑::

您可以更新 displayData 方法以将标签文本作为参数传递。

-(void)displayData: (NSString*) labelText{
        PRIORITYlabel.text = labelText;
}

您可以从 viewDidLoad 和 receiveNotifications: 方法中调用相同的方法。

来自 viewDidLoad::

[self displayData: [NSString stringWithFormat:@"Short Description: \n%@", QDESCRIPTIONstring]];

来自 receiveNotifications::

[self displayData: [NSString stringWithFormat:@"Short Description: \n%@", [[notification userInfo] objectForKey:@"PRIORITY"]]];