我知道这个问题在这里已经被问答了很多次并得到了解答。但是我第一次处理这个问题,仍然不能完全理解如何实现它。下面是代码,我有一个委托方法,用于从 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
,并将其存储在FirstViewController
的NSArray
属性sampleData
中。
不知何故,我无法在FirstViewController
中访问sendDataBackToFirstController
。我还需要实现哪些其他内容才能在那里访问sendDataBackToFirstController
?