Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

iphone - trying to update a UILabel on a parent view controller when dismissing the modal view

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.

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".

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

EDIT: I have to mention 2 things here:

  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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...