Passing data between 2 UIViewController using delegate and protocol
我知道这个问题在这里已经问过很多次了。但是我第一次处理这件事,仍然无法在我的头脑中得到完美的实现。这是我使用委托方法实现的代码,用于将数据从
FirstView控制器.h
1 2 3 4 | #import"SecondViewController.h" @interface FirstViewController : UITableViewController<sampleDelegate> @end |
FirstView控制器.m
1 2 3 4 5 6 7 8 9 10 11 12 13 | @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 |
第二视图控制器
1 2 3 4 5 6 7 | @protocol sampleDelegate <NSObject> - (NSArray*)sendDataBackToFirstController; @end @interface SecondViewController : UITableViewController @property (nonatomic, strong) id <sampleDelegate> sampleDelegateObject; @end |
第二视图控制器.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @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 |
我做得对吗?我只希望它将
不知何故,我无法访问
不太对。首先,您需要在第一个视图控制器中分配委托属性,以便第二个视图控制器知道要向哪个对象发送消息。
FirstView控制器.m
1 | controller.delegate = self; |
第二,向后发送和接收委托方法。它的设置方式是让FirstViewController在第二个控制器上调用
1 2 3 | @protocol sampleDelegate <NSObject> - (void)secondControllerFinishedWithItems:(NSArray* )newData; @end |
然后,当您的第二个VIEW控制器完成它的任务并需要通知它的委托时,它应该这样做:
1 2 3 4 5 | // ... do a bunch of tasks ... // notify delegate if ([self.delegate respondsToSelector:@selector(secondControllerFinishedWithItems:)]) { [self.delegate secondControllerFinishedWithItems:arrayOfNewData]; } |
我在这里添加了一个额外的if语句,以检查委托是否会响应我们想在实际发送之前发送它的方法。如果在我们的协议中有可选的方法,而没有这个方法,应用程序就会崩溃。
希望这有帮助!
请跟这个走
FirstView控制器.h
1 2 3 4 | #import"SecondViewController.h" @interface FirstViewController : UITableViewController<sampleDelegate> @end |
FirstView控制器.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @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 |
第二视图控制器.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @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 |
第一次改变
第二你的协议应该是
1 2 3 | @protocol sampleDelegate <NSObject> - (void)sendDataBackToFirstController:(NSArray*)dataToSendBack; @end |
当您想返回数据时,调用