Ios 使用委托和协议在2 UIViewController之间传递数据
我知道这个问题在这里已经被问过很多次了。但是我第一次处理这个问题,仍然无法在我的脑海中得到完美的实现。下面是我使用委托方法实现的代码,用于将数据从Ios 使用委托和协议在2 UIViewController之间传递数据,ios,uiviewcontroller,delegates,protocols,Ios,Uiviewcontroller,Delegates,Protocols,我知道这个问题在这里已经被问过很多次了。但是我第一次处理这个问题,仍然无法在我的脑海中得到完美的实现。下面是我使用委托方法实现的代码,用于将数据从SecondViewController传递到FirstViewController FirstViewController.h SecondViewController.h 我做得对吗?我希望它将self.dataUnsecondViewController中的数据发送到FirstViewController并将其存储在FirstViewContro
SecondViewController
传递到FirstViewController
FirstViewController.h
SecondViewController.h
我做得对吗?我希望它将self.dataUnsecondViewController
中的数据发送到FirstViewController
并将其存储在FirstViewController
的NSArray
属性sampleData
中
不知何故,我无法访问FirstViewController
中的sendDataBackToFirstController
。在那里访问sendDataBackToFirstController
,我还缺少什么其他东西?第一次更改
@属性(非原子,强)id sampleDelegateObject代码>到
@属性(非原子,弱)id sampleDelegateObject
为了防止保留循环,请使用该词搜索google以进行解释
第二
你的协议应该是
@protocol sampleDelegate <NSObject>
- (void)sendDataBackToFirstController:(NSArray*)dataToSendBack;
@end
@protocol sampleDelegate
-(void)sendDataBackToFirstController:(NSArray*)dataToSendBack;
@结束
当您想发回数据时,可以调用[self.sampleDelegateObject senddatabacktofirstcontrolooler:yourData]代码>第一视图控制器必须在协议中实现这些方法 不太对。首先,需要在第一个视图控制器中指定委托属性,以便第二个视图控制器知道要向哪个对象发送消息
FirstViewController.m
controller.delegate = self;
@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
第二,您可以向后发送和接收委托方法。您的设置方式要求FirstViewController在第二个控制器上调用sendDataBackToFirstController
。在委托模式中,SecondViewController是发送消息并可选地使用该方法发送数据的控制器。因此,您应该将委托声明更改为如下内容:
@protocol sampleDelegate <NSObject>
- (void)secondControllerFinishedWithItems:(NSArray* )newData;
@end
// ... 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
@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
SecondViewController.h
#import "SecondViewController.h"
@interface FirstViewController : UITableViewController<sampleDelegate>
@end
@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
@protocol sampleDelegate
-(NSArray*)发送数据包至第一控制器;
@结束
@接口SecondViewController:UITableViewController
@属性(非原子,强)id sampleDelegateObject;
@结束
SecondViewController.m
@接口SecondViewController()
@属性(强,非原子)NSArray*DataUnsecondViewController;
@结束
@第二视图控制器的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.dataUnsecondViewController=[NSArray arrayWithObjects:@“Object1”,“Object2”,nil];
//调用委托方法
[sampleDelegateObject sendDataBackToFirstController];
}
@结束
您需要将第一个vc设置为第二个vc的代理。因此,第二个vc的委托方法需要在第一个vc中实现。然后在触发事件时从第二个vc调用该方法。从您的场景来看,不清楚您是否想要数据源或委托。如果使用第二个vc作为第一个vc的数据源,则第一个vc请求数据。但当第二个vc中发生事件时,您只想将值返回给第一个vc,然后返回它的委托类型。然后需要将返回类型更改为void,并将数据作为参数传递。Correct。我尝试在didSelectRow中执行controller.delegate=self。。。我的第一个vc。但不知何故,控制器无法找到委托对象。到目前为止,我的代码似乎正确吗?请按照@gdavis和art@NANNAV-不确定这个问题是如何重复的。在我问题的最初一开始,我确实提到它已经得到了回答,但这个问题是针对我的实施的。那里的答案对我实现代码没有多大帮助,这就是为什么我开始了另一个问题。除了协议和委托之外,还有一个选项非常简单且易于实现……协议的实现将在您想要实现的类中。感谢您提供代码的详细信息。Your和@art's解释帮助我理解整个协议流程和代表。我遵循您和art的代码来实现代理,我得到了我想要的。我将委托属性更改为“弱”而不是“强”,以防止保留循环。
@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
@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