Nuibb
Nuibb

Reputation: 1860

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

I want to pass data from detail view controller to master view controller of UISplitViewController for iPad. I have a login view controller as root view before splitView in storyboard. I tried by creating a custom delegate to pass data but it does not work. Data will pass from didSelectRowAtIndexPath method of DetailViewController. Please let me know where i'm doing wrong. Below is my code -

In 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

In 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

In 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

In DelegateToPassDataInMaster.h -

#import "DelegateToPassDataInMaster.h"

@implementation DelegateToPassDataInMaster

-(void) getDataFromDelegateFunc:(NSString *)_id {
    [self.delegate getRowID:_id];
}

@end

Upvotes: 1

Views: 1100

Answers (1)

Ben
Ben

Reputation: 1061

Usually, when making a delegate you implement the protocol on the detail view & you set the delegate of this protocol to the place you want to do work (e.g, the master view).

In your case, you have done this:

@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;

So your delegate is set up, but now when you assign delegateController to something is where you make the mistake.

Here in your master, you have made an object, and set that as the delegate

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

But that has no reference to this in your detail view:

self.delegateController = [DelegateToPassDataInMaster new];

So, you need to setting self.delegateController to your master view controller. To get the reference, you can use

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

inside your master view. I will expand further if you need, but I hope you understand well enough to implement this.

Upvotes: 2

Related Questions