【发布时间】:2023-04-07 11:43:01
【问题描述】:
我知道这个问题已经在这里被问过很多次了。但我是第一次处理这件事,仍然无法在我的脑海中完美地实现它。这是我实现的将数据从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并将其存储在NSArray属性sampleData中FirstViewController中。
不知何故,我无法在FirstViewController 中访问sendDataBackToFirstController。我还缺少什么其他东西来访问sendDataBackToFirstController 那里?
【问题讨论】:
-
您需要将第一个vc设置为第二个vc的代理。所以第二个vc的委托方法需要在第一个vc中实现。然后在触发事件时从第二个 vc 调用该方法。从您的情况来看,如果您想要拥有数据源或委托,则不清楚。如果您使用第二个 vc 作为第一个 vc 的数据源,则它是第一个 vc 请求数据。但是您只想在第二个 vc 中发生某些事件时将值返回给第一个 vc,然后是它的委托类型。然后您需要将返回类型更改为 void 并将数据作为参数传递。
-
正确。我尝试在我的 First vc 的 didSelectRow... 中执行 controller.delegate = self。但不知何故控制器无法找到委托对象。到目前为止,我的代码似乎正确吗?
-
请关注@gdavis和art的回答
-
@NANNAV - 不知道这个问题是如何重复的。我确实在我的问题的最初开始提到它已经得到了回答,但这个问题是特定于我的实现的。那里的答案对我实现我的代码没有多大帮助,这就是我开始另一个问题的原因。
-
除了协议和委托之外,还有一个选项是简单且易于实现的块......
标签: ios uiviewcontroller delegates protocols