Passing data between 2 UIViewController using delegate and protocol
Solution 1
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!
Solution 2
Please follow this
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];
controller.sampleDelegateObject=self;
[self.navigationController pushViewController:controller animated:YES];
}
//implementation of delegate method
- (NSArray*)sendDataBackToFirstController
{
return self.dataInSecondViewController;
}
@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];
//calling the delegate method
[sampleDelegateObject sendDataBackToFirstController];
}
@end
Solution 3
First change
@property (nonatomic, strong) id <sampleDelegate> sampleDelegateObject;
to
@property (nonatomic, weak) id <sampleDelegate> sampleDelegateObject;
to prevent retain cycle search google with that word for explanation.
Second your protocol should be
@protocol sampleDelegate <NSObject>
- (void)sendDataBackToFirstController:(NSArray*)dataToSendBack;
@end
and when you want to send data back you call [self.sampleDelegateObject sendDataBackToFirstControoler:yourData];
first view controller must implement those method in protocol.
Related videos on Youtube
tech_human
Updated on September 23, 2022Comments
-
tech_human over 1 year
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
toFirstViewController
.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
toFirstViewController
and store it over there in theNSArray
propertysampleData
ofFirstViewController
.Somehow I am not able to access
sendDataBackToFirstController
inFirstViewController
. What other things I am missing implementing to accesssendDataBackToFirstController
there?-
Anupdas over 10 yearsYou need to set the first vc as delegate of second vc. So the delegate method of second vc needs to be implemented in first vc. Then invoke the method from second vc when the event is triggered. From your scenario its unclear if you want to have a datasource or delegate. If you use second vc as a datasource for first vc, it is the first vc which requests data. But you just want to return the value back to first vc when some event happens in second vc then its kind of delegate. Then you would need to change the return type to void and pass the data as argument.
-
-
Albin Joseph over 10 yearsprotocol's implementation will be in the class where you want to implement
-
tech_human over 10 yearsYour and @art's explanation helped me a lot in understanding the entire flow of the protocol and delegates. I followed your and art's code to implement the delegates and I got what I wanted. I changed the delegate property to weak instead of strong to prevent the retain cycle.
-
Swap-IOS-Android over 9 yearsThank you for detail information with code.