使用委托和协议在2 UIViewController之间传递数据

我知道这个问题已经在这里被问及过很多次了。 但是,我是第一次处理这个事情,仍然无法在脑海中得到完美的实现。 这里的代码我有委托方法我实现从SecondViewController传递数据到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 

我做得对吗? 我希望它将self.dataInSecondViewController的数据发送到FirstViewController并将其存储在FirstViewControllerNSArray属性FirstViewController

不知何故,我无法访问sendDataBackToFirstController中的FirstViewController 。 还有什么其他的东西我想实现访问sendDataBackToFirstController吗?

不太对。 首先,您需要在第一个视图控制器中分配委托属性,以便第二个视图控制器知道将消息发送到哪个对象。

FirstViewController.m

 controller.delegate = self; 

其次,你有向后发送和接收你的委托方法。 您可以通过sendDataBackToFirstController在第二个控制器上调用sendDataBackToFirstController的方式进行设置。 在委托模式中,SecondViewController是发送消息并可select使用该方法发送数据的模式。 所以,你应该改变你的委托声明是这样的:

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

然后,当你的SecondViewController完成其任务,并需要通知其委托,它应该做这样的事情:

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

我在这里添加了一个额外的if语句,以确保委托在实际发送之前将响应我们想要发送的方法。 如果我们的协议中有可选的方法,并没有这个,应用程序会崩溃。

希望这可以帮助!

请按照这个

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 

首先改变@property (nonatomic, strong) id <sampleDelegate> sampleDelegateObject; to @property (nonatomic, weak) id <sampleDelegate> sampleDelegateObject; 以防止保留周期search谷歌用这个词来解释。

其次你的协议应该是

 @protocol sampleDelegate <NSObject> - (void)sendDataBackToFirstController:(NSArray*)dataToSendBack; @end 

当你想发送数据时,你可以调用[self.sampleDelegateObject sendDataBackToFirstControoler:yourData]; 第一视图控制器必须在协议中实现这些方法。