如何在使用UISplitViewController的详细视图控制器中获取主视图控制器?

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

} 

完美的想法 :),非常感谢。 - simply_me

0

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接