在解除模态视图时尝试更新父视图控制器上的UILabel [英] trying to update a UILabel on a parent view controller when dismissing the modal view

查看:140
本文介绍了在解除模态视图时尝试更新父视图控制器上的UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在有人在模态视图中进行更改后更新父视图中的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:


  1. 在这种情况下,最好使用共享数据模态保存数据,以便在任何视图中访问此数据你的计划。换句话说,将数据与类分开是一种很好的做法。

  2. 请记住重新调整在main中使用的 NSNotificationCenter 通过添加 [[NSNotificationCenter defaultCenter] removeObserver:self];

  1. In this scenario it is always preferable to have Shared Data Modal where you save your data in so you can access this data in any view in your program. In other words it is a good practice to separate the data from classes.
  2. Remember to resomve the NSNotificationCenter that you used in the main view by adding [[NSNotificationCenter defaultCenter] removeObserver:self];

这篇关于在解除模态视图时尝试更新父视图控制器上的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆