How to get master view controller in detail view controller with UISplitViewController?
Asked Answered
S

2

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 ?
} 
Sullen answered 5/3, 2013 at 7:27 Comment(0)
B
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];

} 
Boost answered 5/3, 2013 at 9:25 Comment(0)
B
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.

Bravo answered 31/10, 2014 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.