【问题标题】:How to pass data from detail to master in UISplitViewController for iPad如何在 iPad 的 UISplitViewController 中将数据从细节传递到主控
【发布时间】:2015-11-11 02:26:36
【问题描述】:

我想将数据从详细视图控制器传递到 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

【问题讨论】:

  • 嘿 Nuibb,我认为这是因为您没有对主控制器进行真正的引用,而是在创建一个新对象。要我扩展吗?
  • 感谢您的回复。你能详细解释一下吗?我真的不明白。我该怎么办?
  • 在我的 AppDelegate.m 中,我删除了所有与 spliteView 相关的行。如- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; }
  • 是的,我可以解释一下,给我几分钟时间
  • 查看我的答案以获得更简单的解决方案:stackoverflow.com/questions/24813442/…

标签: ios ipad delegates uisplitviewcontroller master-detail


【解决方案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];

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

【讨论】:

  • 这对 Nuibb 有意义吗?
  • 其实我并不清楚。请让我知道如何在我的主视图中设置 self.delegateController。我删除了“self.delegateController = [[DelegateToPassDataInMaster alloc] init];”在 master 并添加“self.delegateController = self.splitViewController.viewControllers[1];”详细视图控制器。但它崩溃了。
  • 好的,所以需要导入MasterViewController.h,然后做self.delegateController = (MasterViewController*) self.splitViewController.viewControllers[1];
  • 你不需要:"@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;"在主视图中
  • 对不起,它不起作用。我认为我在主视图中设置委托是错误的,无法找到。我很抱歉无法做到这一点。你能从这里查看整个代码吗? dropbox.com/s/87ogi7rfag7ebsq/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 1970-01-01
  • 2013-10-31
  • 2014-01-07
相关资源
最近更新 更多