我有一个加载表视图的navigationVC。当您点击一个单元格时,它会将一些数据复制到detailsVC,还有一个更新按钮来更新这些值。如何刷新详细信息VC中的数据而不返回到tableview并在那里更新?
这就是我想要做的事情:
的TableView>&的DetailsView GT; EditDetails>使用更新的信息返回DetailsView
不是这个:
TableView> DetailsView> EditDetails> DetailsView(通过导航返回)> TableView>(拉到刷新)> 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为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"];
});
}
}
如何让词典覆盖初始数据呢?
答案 0 :(得分:0)
您可以使用NSNotificationCenter
。从编辑视图发布通知,并从表视图和详细信息视图中订阅相同的通知。
发布通知::
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:nil userInfo: userInfo];
如果您想从编辑视图传递任何数据,您可以使用userInfo
字典!!
从表格视图和详细信息视图订阅通知::
[[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];
}
您可以在表格视图和详细视图中添加此操作方法。
在表格视图和详细信息视图中删除观察者::
- (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"]]];