iPad 如何在 UISplitViewController 中将数据从细节传递到主控

How to pass data from detail to master in UISplitViewController for iPad

我想将数据从详细视图控制器传递到 iPad 的 UISplitViewController 的主视图控制器。在情节提要中的 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

通常,在创建委托时,您在详细视图上实现协议,并将该协议的委托设置到您想要工作的地方(例如,主视图)。

在你的情况下,你已经这样做了:

@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;

因此您的委托已设置,但现在当您将 delegateController 分配给某物时,您就犯了错误。

在你的 master 中,你创建了一个对象,并将其设置为委托

self.delegateController = [[DelegateToPassDataInMaster alloc] init];
self.delegateController.delegate = self;

但是在您的详细视图中没有提到这个:

self.delegateController = [DelegateToPassDataInMaster new];

因此,您需要为主视图控制器设置 self.delegateController。要获得参考,您可以使用

self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

在您的主视图中。如果您需要,我会进一步扩展,但我希望您能充分理解以实现这一点。