Here I’ve got a MVC on the left, wanting to know the value of the selected date from the MVC on the right.

Here’s how you create/define a protocol (DatePicker) for the MVC on the right, and then assign yourself as the delegate for the MVC on the left.

Define the protocol and create a delegate property for it:

DatePickerViewController.h

#import "ViewController.h"

@protocol DatePickerDelegate <NSObject>
- (void)didPickDateWithSelectedDate:(NSDate *)selectedDate;
@end

@interface DatePickerViewController : UIViewController
@property (nonatomic, weak) id <DatePickerDelegate> delegate;
@end

Synthesize your delegate, and then call it when you want the call back to happen.

DatePickerViewController.m

@synthesize delegate = _delegate;

- (IBAction)datePicked:(UIDatePicker *)sender
{
    [self.delegate didPickDateWithSelectedDate:sender.date];
}

With our protocol/delegate created and designed, we are now ready to switch the the other side, and register ourselves for the callback for the MVC on the left.

First we need to say we implement the protocol interface.

ViewController.m

@interface ViewController () <DatePickerDelegate>

Then we need to actually implement it.

- (void)didPickDateWithSelectedDate:(NSDate *)selectedDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
    
    self.dateLabel.text = formattedDateString;
    NSLog(@"didPickAlarmTimeWithSelectedDate: %@", formattedDateString);
}

Then we need to register ourselves to act as the delegate for the DatePickerMVC on the right. A good place to do this is in the prepareForSegue method.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"dateSegue"]) {
        UIViewController *newController = segue.destinationViewController;
        DatePickerViewController *dateVC = (DatePickerViewController *) newController;
        dateVC.delegate = self;
    }
}

Note for this to work the segue needs an id “dateSegue” which you set by selecting the segue in your story board and assigning it the value.

Run it. And Voila! You got one MVC passing data to another via protocols.

Here is the code in it’s entirety.

DatePickerViewController.h

#import "ViewController.h"

@protocol DatePickerDelegate <NSObject>
- (void)didPickDateWithSelectedDate:(NSDate *)selectedDate;
@end

@interface DatePickerViewController : UIViewController
@property (nonatomic, weak) id <DatePickerDelegate> delegate;
@end

DatePickerViewController.m

#import "DatePickerViewController.h"

@interface DatePickerViewController ()

@end

@implementation DatePickerViewController

@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (IBAction)datePicked:(UIDatePicker *)sender
{
    [self.delegate didPickDateWithSelectedDate:sender.date];
    NSLog(@"datePicked: %@", sender.date);
}

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@end

ViewController.m

#import "ViewController.h"
#import "DatePickerViewController.h"

@interface ViewController () <DatePickerDelegate>

@end

@implementation ViewController
@synthesize dateLabel;

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

- (void)viewDidUnload
{
    [self setDateLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)didPickDateWithSelectedDate:(NSDate *)selectedDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
    
    self.dateLabel.text = formattedDateString;
    NSLog(@"didPickAlarmTimeWithSelectedDate: %@", formattedDateString);
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"dateSegue"]) {
        UIViewController *newController = segue.destinationViewController;
        DatePickerViewController *dateVC = (DatePickerViewController *) newController;
        dateVC.delegate = self;
    }
}

@end
Advertisement