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

Passing data between 2 UIViewController using delegate and protocol(使用委托和协议在 2 个 UIViewController 之间传递数据)

本文介绍了使用委托和协议在 2 个 UIViewController 之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经在这里被问过很多次了.但我是第一次处理这件事,但仍然无法在我的脑海中完美地实现它.这是我实现的委托方法的代码,用于将数据从 SecondViewController 传递到 FirstViewController.

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

我做得对吗?我只希望它将 self.dataInSecondViewController 中的数据发送到 FirstViewController 并将其存储在 NSArray 属性 sampleDataFirstViewController 的代码>.

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.

不知何故,我无法访问 FirstViewController 中的 sendDataBackToFirstController.我还缺少什么其他东西来访问 sendDataBackToFirstController 那里?

Somehow I am not able to access sendDataBackToFirstController in FirstViewController. What other things I am missing implementing to access sendDataBackToFirstController there?

推荐答案

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

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

FirstViewController.m

controller.delegate = self;

其次,您可以向后发送和接收委托方法.您以 FirstViewController 预计在第二个控制器上调用 sendDataBackToFirstController 的方式设置它.在委托模式中,SecondViewController 是发送消息并可选择使用该方法发送数据的那个.所以,你应该把你的委托声明改成这样:

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

然后,当您的 SecondViewController 完成其任务并需要通知其委托时,它应该执行以下操作:

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];
}

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

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.

希望这会有所帮助!

这篇关于使用委托和协议在 2 个 UIViewController 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用委托和协议在 2 个 UIViewController 之间传递数据