我想将数据从详细视图控制器传递到UISplitViewController for iPad的主视图控制器。我在故事板中的splitView之前有一个登录视图控制器作为根视图。我尝试通过创建自定义委托来传递数据,但它不起作用。 数据将从DetailViewController的didSelectRowAtIndexPath方法传递。请让我知道我在哪里做错了。以下是我的代码 -
在MasterViewController.m中 -
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController ()
@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;
@property (strong, nonatomic) NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
- (void) getRowID:(NSString *)_id {
NSLog(@"Row ID : %@", _id);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.delegateController = [[DelegateToPassDataInMaster alloc] init];
self.delegateController.delegate = self;
self.objects = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",@"Three", nil];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *str = self.objects[indexPath.row];
cell.textLabel.text = str;
return cell;
}
@end
在DetailViewController.m中 -
#import "DetailViewController.h"
#import "DelegateToPassDataInMaster.h"
@interface DetailViewController ()
@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;
@property (strong, nonatomic) NSMutableArray *objects;
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.delegateController = [DelegateToPassDataInMaster new];
self.objects = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *str = self.objects[indexPath.row];
cell.textLabel.text = str;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = self.objects[indexPath.row];
[self.delegateController getDataFromDelegateFunc:str];
}
@end
在DelegateToPassDataInMaster.h中 -
#import <Foundation/Foundation.h>
@protocol DelegateForPassDataToMasterView;
@interface DelegateToPassDataInMaster : NSObject
@property (nonatomic, weak) IBOutlet id <DelegateForPassDataToMasterView> delegate;
-(void) getDataFromDelegateFunc:(NSString *)_id;
@end
#pragma mark -
@protocol DelegateForPassDataToMasterView <NSObject>
@optional
- (void)getRowID:(NSString *)_id;
@end
在DelegateToPassDataInMaster.h中 -
#import "DelegateToPassDataInMaster.h"
@implementation DelegateToPassDataInMaster
-(void) getDataFromDelegateFunc:(NSString *)_id {
[self.delegate getRowID:_id];
}
@end
答案 0 :(得分:1)
通常,在创建委托时,您需要在详细视图上实现协议。您将此协议的委托设置为您要工作的位置(例如,主视图)。
在您的情况下,您已完成此操作:
@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;
所以你的代表已经设置完毕,但是现在当你将delegateController
分配给某个东西时,你犯了错误。
在您的主人中,您创建了一个对象,并将其设置为委托
self.delegateController = [[DelegateToPassDataInMaster alloc] init];
self.delegateController.delegate = self;
但是在您的详细视图中没有提到这一点:
self.delegateController = [DelegateToPassDataInMaster new];
因此,您需要将self.delegateController设置为主视图控制器。要获得参考,您可以使用
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
在主视图中。如果需要,我会进一步扩展,但我希望你能够很好地理解这一点。