1

I am having a hard time communicating data between two view controllers that are inside a UISplitViewController. I am following this tutorial. I was able to create a split view controller with UITableViews on both master and detail views. Now, What I really want is that when I tap on a particular row in the master table, it has to send some value to the detail view.

I am just playing around with a custom delegate to pass some value from one view controller to another to see if there is any communication between them but nothing seems to work any way.

In MasterTableView.h

@protocol sendingProtocol <NSObject>

-(void)passSomeValue:(NSString *)someValue;

@end



@interface MasterTableView : UITableViewController
{
    NSArray *menuArray;
    id<sendingProtocol>delegate;
}

@property (nonatomic,assign) id<sendingProtocol>mydelegate;

@end

Synthesized in .m file.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[self mydelegate] passSomeValue:@"Some Value"];
}

In DetailTableView.h

-(void)passSomeValue:(NSString *)someValue
{
    NSLog(@"%@", someValue);
}

Please note that I am calling the mydelegate inside the ViewDidLoad method. Is this the write way? Can someone help?

- (void)viewDidLoad
{
    [super viewDidLoad];
    MasterTableView *masterView = [[MasterTableView alloc] init];
    masterView.mydelegate = self;
}

Thank you in advance!

GenieWanted
  • 4,473
  • 4
  • 24
  • 35
  • Possible Duplicate of http://stackoverflow.com/questions/8031238/uisplitviewcontroller-master-detail-communication – Puneet Sharma Sep 16 '13 at 14:15
  • @Puneet I have gone through the question before I posted this one. Ray Wanderlich's tutorial uses Storyboard but I am just using Xib. A lot of tutorials on the internet are using Storyboards. Thank you for your help. – GenieWanted Sep 16 '13 at 14:19
  • Check out [this answer!](http://stackoverflow.com/questions/33721081/swift-how-to-detect-if-uisplitviewcontroller-is-currently-showing-1-or-2-contro/38547858#38547858) It might give you some insight. – Jon Vogel Jul 24 '16 at 01:15

1 Answers1

1

In viewDidLoad method of your DetailTableView you should not create a new MasterTableView object. The error is here in this method:

- (void)viewDidLoad
{
    [super viewDidLoad];
    MasterTableView *masterView = [[MasterTableView alloc] init];
    masterView.mydelegate = self;
}

You are creating another object of MasterTableView and setting its delegate to self and hence all the problem.

To set the delegate of MasterTableView to DetailTableView, go to AppDelegate.h. You must have defined the MasterTableView and DetailTableView objetcs in AppDelegate.

 //Set the DetailTableView as the master's delegate.
self.masterTableView.delegate = self.detailTabelView;
pkamb
  • 33,281
  • 23
  • 160
  • 191
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • I have done what you suggested. Still no luck! :( I have my custom delegate working on my other controllers. This does not seem to work only on UISplitview. Is there any relevance to that? Is it good to set the delegate in ViewDidLoad? Because both of these master and detail viewcontrollers seem to load at the same time in UISplitView. Does it make any impact? THanks. – GenieWanted Sep 17 '13 at 04:41
  • I wasn't able to make the delegate work but instead I used NotificationCenter to get things done. Now, all good. I really appreciate your help. +1 Thank you so much! – GenieWanted Sep 17 '13 at 16:36
  • Did you try the edit? Although Notifications can also help you, this task is best handled by delegates. – Puneet Sharma Sep 18 '13 at 17:11