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 t
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)
}
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:
NSNotificationCenter
that you used in the main view by adding [[NSNotificationCenter defaultCenter] removeObserver:self];
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.