8

我有一个UISplitViewController主控制器和详细控制器:

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; 

MyDetailController是一个表格列表视图控制器,我希望主视图控制器在用户单击单元格时运行一种方法,那么如何获取主控制器详细控制器?

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [master some_method]; // how to get ?
} 
4

2 回答 2

15

我会改用通知,所以在你的主人:

-(void) viewDidLoad {

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

}

-(void) someMethod {

    ...

}

并在您的详细信息中:

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

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

} 
于 2013-03-05T09:25:24.030 回答
0

虽然 jjv360s 的回答有很大帮助,但有些人肯定想将值传递给被调用的方法。以下教程帮助了我:

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

要传递 value value,您需要value在发送通知的类中公开为属性。接收方法需要将.(id)转换为暴露value.

于 2014-10-31T11:21:09.540 回答