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!