使用委托和协议在2 UIViewController之间传递数据 [英] Passing data between 2 UIViewController using delegate and protocol

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

问题描述

我知道这个问题已经在这里被问及多次了。但是我第一次处理这件事情,仍然无法在我心目中完全实现这一点。这里是代码,我有代理方法,我将数据从 SecondViewController 传递到 FirstViewController



FirstViewController.h

  #importSecondViewController.h

@interface FirstViewController:UITableViewController< sampleDelegate>
@end

FirstViewController.m

  @interface FirstViewController()

//我要存储数据的数组我从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 属性 sampleData FirstViewController



不知怎的,我无法访问 FirstViewController 中的 sendDataBackToFirstController 。还有什么其他的事情实现访问 sendDataBackToFirstController 那里?

解决方案

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



FirstViewController.m

  controller.delegate = self; 

其次,您有向后发送和接收您的委托方法。您可以通过FirstViewController预期在第二个控制器上调用 sendDataBackToFirstController 的方式进行设置。在委托模式中,SecondViewController是发送消息的选项,并可选择使用该方法发送数据。因此,您应该将您的委托声明更改为以下内容:

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

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

  // ...做一堆任务... 
//通知委托
if([self.delegate respondToSelector:@selector(secondControllerFinishedWithItems :)]){
[self.delegate secondControllerFinishedWithItems:arrayOfNewData];
}

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



希望这有帮助!


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

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.

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

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!

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆