3

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!

TheTC
  • 677
  • 9
  • 19
  • I would make a delegation method. When the "Save" button is pressed, the modal is dismissed and the delegation method is called. The parent view has this delegation method implemented and when it is called, the label is refreshed. – simonbs Jan 18 '12 at 21:06

3 Answers3

8

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];
antf
  • 3,162
  • 2
  • 26
  • 33
3

To elaborate on my comment. This is how I would implement a delegation method to update the label.

In the header of the parent view controller:

#import "ModalViewController.h"

@interface ViewController : UIViewController <ModalViewControllerDelegate>

/* This presents the modal view controller */
- (IBAction)buttonModalPressed:(id)sender;

@end

And in the implementation:

/* Modal view controller did save */
- (void)modalViewControllerDidSave:(ModalViewController *)viewController withText:(NSString *)text
{
    NSLog(@"Update label: %@", text);
}

/* Prepare for segue */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"modalSegue"])
    {
        ModalViewController *mvc = (ModalViewController *) segue.destinationViewController;
        mvc.delegate = self;
    }
}

/* Present modal view */
- (IBAction)buttonModalPressed:(id)sender
{
    [self performSegueWithIdentifier:@"modalSegue" sender:self];
}

Here you see the delegation method in the top.

The header of the modal view controller would contain the delegation protocol like this:

@protocol ModalViewControllerDelegate;

@interface ModalViewController : UIViewController

@property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;

- (IBAction)buttonSavePressed:(id)sender;

@end

@protocol ModalViewControllerDelegate <NSObject>
- (void)modalViewControllerDidSave:(ModalViewController *)viewController withText:(NSString *)text;
@end

The implementation of the modal view controller would contain a method similar to this one:

/* Save button was pressed */
- (IBAction)buttonSavePressed:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(modalViewControllerDidSave:withText:)])
        [self.delegate modalViewControllerDidSave:self withText:@"Some text"];

    [self dismissModalViewControllerAnimated:YES];
}

When the save button is pressed, the delegate is notified and the text in your text view is sent through the delegation method.

simonbs
  • 7,932
  • 13
  • 69
  • 115
  • This seems very solid. Thank you very much. About to test this out now. – TheTC Jan 18 '12 at 21:31
  • Thanks. Might want to check out [this answer](http://stackoverflow.com/a/6169104/426839) for a fuller explanation with some strong suggestions such as always making delegate properties weak references. – Jeff Bowen Feb 24 '15 at 19:01
  • True, the delegate should definitely be weak. That was a mistake. I have updated the answer. Thanks. – simonbs Feb 24 '15 at 22:28
2

in SWIFT:

ParentViewController :

    func updateLabel() {
        yourLabel.text! = "what you need"
    }

override func viewDidLoad() {
        super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.updateLabel), name: "DoUpdateLabel", object: nil) 
}

In OtherView:

@IBAction func closePopUp(sender: AnyObject) {

       NSNotificationCenter.defaultCenter().postNotificationName("DoUpdateLabel", object: nil, userInfo: nil)
    }
Done
  • 1,088
  • 2
  • 11
  • 19