8

I have a UISplitViewController with master controller and detail controller:

MyMasterController *masterViewController = [[[MyMasterController alloc] initWithDirectory:directoryElement] autorelease];
MyDetailController *detailViewController = [[MyDetailController alloc] init];

masterViewController.detailViewController = detailViewController;

UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = @[[[UINavigationController alloc] initWithRootViewController:masterViewController], [[UINavigationController alloc] initWithRootViewController:detailViewController]];
splitViewController.delegate = self; 

The MyDetailController is a table list view controller, I want to master view controller run one method when user clicks on cell, So how to get the master controller in detail controller ?

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [master some_method]; // how to get ?
} 
klefevre
  • 8,595
  • 7
  • 42
  • 71
why
  • 23,923
  • 29
  • 97
  • 142

2 Answers2

15

I would use notifications instead, so in your master:

-(void) viewDidLoad {

    ...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod) name:@"DoSomeMethod" object:nil];

}

-(void) someMethod {

    ...

}

And in your detail:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   [[NSNotificationCenter defaultCenter] postNotificationName:@"DoSomeMethod" object:nil];

} 
jjv360
  • 4,120
  • 3
  • 23
  • 37
0

While jjv360s answer helps a lot, some surely want to pass values to the called method. Following tutorial has helped me:

http://www.devfright.com/nsnotificationcenter-tutorial/

To pass the value value, you need to expose value as a property in the class that sends the notification. The receiving method needs to cast the (id)to the class that exposes the value.

Jan Bühler
  • 421
  • 3
  • 18