2013-03-05 95 views
8

我有一個主控制器和細節控制器UISplitViewController如何使用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 ? 
} 

回答

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

} 
+0

這是一個好主意,我認爲 – why 2013-03-05 10:05:04

+0

謝謝你這麼做。正是我在找什麼。輝煌!! – 2013-03-21 14:09:48

+0

完美的主意:)。非常感謝 – 2014-06-18 05:20:27

0

雖然jjv360s答案有很大幫助,一些肯定要傳值到被調用的方法。 下面的教程幫助了我:

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

要通過價值value,你需要公開value在發送通知的類的屬性。接收方法需要將(id)轉換爲公開value的類。

相關問題