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!
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;