关闭模式视图时尝试更新父视图控制器上的 UILabel [英] trying to update a UILabel on a parent view controller when dismissing the modal view
问题描述
在有人更改模态视图后,我尝试更新父视图中的 UILabel.所以,在他们点击保存"之后……新输入的值会改变父视图控制器上显示的文本.
I am trying to update a UILabel in a parent View after someone makes a change in a modal view. So, after they click "save" ... the newly entered value would change what text is displayed on the parent view controller.
但是,我似乎无法让 UILabel 刷新新输入的值.
But, I can't seem to get that UILabel to refresh the newly entered value.
关于我可以尝试什么的任何想法?我已经尝试了一些东西,但由于视图已经加载,没有任何东西被刷新".
Any ideas on what I can try? I've tried a few things, but being the view is already loaded, nothing is getting "refreshed".
谢谢!
推荐答案
有很多方法可以做到这一点.一种方法是使用 NSNotificationCenter
能够在不同的类之间进行调用.因此,在父视图中,您将拥有一个负责更新的函数(我们称之为 updateLabel),您将执行以下操作:
There are many ways to do this. One way is to use NSNotificationCenter
to be able to do calls between different classes. So in the parent view you will have a function responsible for the update (lets call it updateLabel) and you will do the following:
- (void) updateLabel
{
yourLabel.text = @"what you need";
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel) name:@"DoUpdateLabel" object:nil];
}
现在在其他视图中只需在保存按钮中发布通知:
Now in other view simply post a notification in the save button:
[[NSNotificationCenter defaultCenter] postNotificationName:@"DoUpdateLabel" object:nil userInfo:nil];
我必须在这里提两件事:
I have to mention 2 things here:
- 在这种情况下,最好使用共享数据模式来保存数据,以便您可以在程序的任何视图中访问这些数据.换言之,将数据与类分开是一种很好的做法.
- 记得通过添加
[[NSNotificationCenter defaultCenter] removeObserver:self];
来解决你在主视图中使用的
NSNotificationCenter
这篇关于关闭模式视图时尝试更新父视图控制器上的 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!