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

标签 ios ipad delegates uisplitviewcontroller master-detail

我想将数据从详细 View Controller 传递到 iPad 的 UISplitViewController 的主视图 Controller 。在 Storyboard 中的 splitView 之前,我有一个登录 View Controller 作为 Root View 。我尝试通过创建自定义委托(delegate)来传递数据,但它不起作用。 数据将从 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

最佳答案

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

在你的例子中,你已经这样做了:

@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;

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

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

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

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

self.delegateController = [DelegateToPassDataInMaster new];

因此,您需要将 self.delegateController 设置为您的主视图 Controller 。要获得引用,您可以使用

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

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

关于ios - 如何在 iPad 的 UISplitViewController 中将数据从细节传递到主控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067527/

相关文章:

ios - 在 iOS 上的 SceneKit 中导入 3d 模型

ios - 如何制作带有步骤、箭头样式的 UIToolbar

.NET:如何在 BeginInvoke 回调期间与表单对话?

c# - 保留对接口(interface)方法的引用并将其应用于该接口(interface)的实现

iphone - 服务器无法识别 iPhone 中 HTTP header SOAPAction 的值?

ios - 简单地继承 UITouchGestureRecognizer,设置 minimumPressDuration

ios - 在没有启动屏幕的情况下强制 iPad Pro 达到全分辨率

css - 旧版 iPhone/iPad 上的特定链接不是 "clickable"

iOS实际RAM和ProcessInfo.processInfo.physicalMemory之间的差异稳定吗?

swift - 编辑- UITableView 单元格按钮操作委托(delegate)- Swift 4