Passing data between 2 UIViewController using delegate and protocol

delegates, ios, protocols, uiviewcontroller

Solution

Not quite right. First you need to assign the delegate property in the first view controller so the second view controller knows which object to send messages to.

FirstViewController.m

controller.delegate = self;

Second, you have the sending and receiving of your delegate method backwards. You have it setup in a way where the FirstViewController is expected to call `sendDataBackToFirstController` on the second controller. In a delegate pattern, the SecondViewController is the one that sends the message and optionally sends data with that method. So, you should change your delegate declaration to something like this:

@protocol sampleDelegate <NSObject>
- (void)secondControllerFinishedWithItems:(NSArray* )newData;
@end

Then, when your SecondViewController finishes its tasks and needs to notify its delegate, it should do something like this:

// ... do a bunch of tasks ...
// notify delegate
if ([self.delegate respondsToSelector:@selector(secondControllerFinishedWithItems:)]) {
    [self.delegate secondControllerFinishedWithItems:arrayOfNewData];
}

I added an extra if statement here to check to make sure the delegate will respond to the method we want to send it before actually sending it. If we had optional methods in our protocol and did not have this, the app would crash.

Hope this helps!

Problem

I know this question has been asked and answered many times over here. But I am dealing with this thing for the first time and still not able to get the perfect implementation of it in my mind. Here's the code I have the delegate method I implement to pass data from `SecondViewController` to `FirstViewController`. FirstViewController.h ``` #import "SecondViewController.h" @interface FirstViewController : UITableViewController<sampleDelegate> @end ``` FirstViewController.m ``` @interface FirstViewController () // Array in which I want to store the data I get back from SecondViewController. @property (nonatomic, copy) NSArray *sampleData; @end @implementation FirstViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SecondViewController *controller = [[SecondViewController alloc] init]; [self.navigationController pushViewController:controller animated:YES]; } @end ``` SecondViewController.h ``` @protocol sampleDelegate <NSObject> - (NSArray*)sendDataBackToFirstController; @end @interface SecondViewController : UITableViewController @property (nonatomic, strong) id <sampleDelegate> sampleDelegateObject; @end ``` SecondViewController.m ``` @interface SecondViewController () @property (strong, nonatomic) NSArray *dataInSecondViewController; @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.dataInSecondViewController = [NSArray arrayWithObjects:@"Object1", @"Object2", nil]; } - (NSArray*)sendDataBackToFirstController { return self.dataInSecondViewController; } @end ``` Am I doing it correctly? All I want it to send the data in `self.dataInSecondViewController` to `FirstViewController` and store it over there in the `NSArray` property `sampleData` of `FirstViewController`. Somehow I am not able to access `sendDataBackToFirstController` in `FirstViewController`. What other things I am missing implementing to access `sendDataBackToFirstController` there?

Original source

Related problems