I have two viewController and iam using a delegate method to update a label in the first view controller with text entered in the second viewcontroller.
My problem is the value entered in the textfield(secondViewController) is not getting pushed into the UILabel(FirstVIewController)
My First ViewController.h
#import "SecondViewController.h"
@interface ViewController : UIViewController <CustomDelegate>
@end
First ViewController.m
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@end
@implementation ViewController
-(void)sendinfo:(NSString *)info
{
_infoLabel.text = info;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"info"])
{
SecondViewController *obj = segue.destinationViewController;
obj.userDelegate = self;
}
}
SecondViewController.h
@protocol CustomDelegate <NSObject>
-(void)sendinfo:(NSString *)info;
@end
@interface SecondViewController : UIViewController
@property (weak, nonatomic) id <CustomDelegate> userDelegate;
@property (weak, nonatomic) IBOutlet UITextField *textField;
SecondViewController.m
- (IBAction)sendInfo:(id)sender {
[self.userDelegate sendinfo:_textField.text];
UIViewController * controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Home"];
[self presentViewController:controller animated:YES completion:Nil];
}
You are creating another UIViewController in your SecondViewController and the delegate is set to the viewController from where you are coming.
To see your changes, you should do a [self dismissViewControllerAnimated:YES completion:nil];
in your SecondViewController
Example:
- (IBAction)sendInfo:(id)sender {
[self.userDelegate sendinfo:_textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}