We can use a delegate to pass the data back to previous Controller. So if You are  in your NextViewController  and you need to pass the data to VieController than  you need to create a protocol that sends data back to your ViewController. Your ViewController  would become a delegate of NextViewController.

Steps:

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name ( Lets say ProtocolDemo).
  3. Click next button and the Project will be created.
  4. Now go to Your StoryBoard , Click on the View Controller. Then Click on the Xcode options”Editor” and then embed a navigation Controller.  Take a label and a button on the ViewController and make proper connections. Your ViewController.h and ViewController.m will look something like this

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *outputLabel;

@end

 

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

– (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

– (IBAction)goNextButtonAction:(id)sender {

}

@end

5. Now create a new class of subtype UIViewController and name it”NextViewController”.

6. Now go to StoryBoard and take a ViewController from the object library and change the class name to “NextViewController”.

7. Take a UITextField  and make proper connection of it. Also connect the delegate.

8. Now we will create a protocol in NextViewController.h.

#import <UIKit/UIKit.h>

@protocol sendDataBack <NSObject>
-(void)sendDataToPreviousController: (NSString *)string;

@end

@interface NextViewController : UIViewController
@property (assign,nonatomic) id delegate;

@end

9. Now go to NextViewController.m and write the implementation.

#import “NextViewController.h”

@interface NextViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;

@end

@implementation NextViewController

– (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

– (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
   
    [_delegate sendDataToPreviousController:textField.text];
}
/*
#pragma mark – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

10. Now go to ViewController.m and write the action for the button and delgate method implementation

– (IBAction)goNextButtonAction:(id)sender {
    NextViewController *acontollerobject=[self.storyboard instantiateViewControllerWithIdentifier:@”NVCSID”];

    acontollerobject.delegate=self; // protocol listener
    [self.navigationController pushViewController:acontollerobject animated:YES];
}
-(void)sendDataToPreviousController: (NSString *)string{
    NSLog(@”Fired”);
    self.outputLabel.text = string;
}

11. Now run the code and click the button to go to nextViewController. Their write somrthing in textfield and then click the back button, the content of textfield will be shown in the label in the ViewController. Enjoy Coding 🙂

Advertisements