iosuisplitview

How to get master view controller in detail view controller with UISplitViewController?


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 ?
} 

Solution

  • 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];
    
    }