2010-11-04 79 views
2

我在UISplitViewController中有一個自定義的customUIViewController,並想從另一個類的detailView(這是UISplitViewController中的另一個UIViewController)訪問customUiViewController的實例;我怎樣才能做到這一點?如何從另一個類訪問UISplitViewController中的自定義UIViewController

CODE SNIP(別擔心有關語法;它是縮短了)

myAppDelegate.m

customViewController *masterView = [[customViewController alloc] init; 
UINavigationController *NVC = [[UINavigationController alloc] initWithRootViewController:masterView]; 

MYViewController *detailView = [[MyViewController alloc] init; 

UISplitViewController *mySplit = [...]; 

mySplit.viewControllers = NSArray[...masterview,detailView,nil]; 

[window addSubView:mySplit view]; 

MyViewController.m

-(void) someMethod { 
     customViewController *myInstance = (customViewController)[self.splitViewController objectAtIndex:0]; ?? 
// I think this just gets the outter UINavigationController 
     [myInstance doSomething]; 
} 

customViewController.m

-(void) doSomething { 
} 

I希望能夠訪問customViewController來調用doSomething方法。無論customViewController和myViewController相同UISplitViewController

回答

2

如果你正在使用默認UISplitView工作XCode所做的,你需要引用AppDelegate來獲得splitView的ivar:

YourAppDelegate *del = (YourAppDelegate *)[[UIApplication sharedApplication]delegate]; 
UISplitViewController *split = del.splitViewController; 
NSArray *vcArray = split.viewControllers; 
//left is objectAtIndex:0, right is objectAtIndex:1 
+0

這個工程!謝謝 – Arcadian 2010-11-07 00:21:34

+0

爲什麼我只在split.viewControllers中有一個。在iPad上工作,但不適用於iPhone – Gank 2014-12-17 09:10:30

5

UIViewControllers有splitViewController屬性,因此嘗試使用,要得到一個內部參考:

customViewController *myInstance = 
    (customViewController *)[self.splitViewController.viewControllers 
           objectAtIndex:0]; 

指數0是在拆分視圖控制器左側視圖控制器。

編輯:
如果左側視圖控制器是一個UINavigationController,然後得到的根視圖控制器,這樣做:

UINavigationController *nc = 
    (UINavigationController *)[self.splitViewController.viewControllers 
           objectAtIndex:0]; 

customViewController *myInstance = 
    (customViewController *)[nc.viewControllers objectAtIndex:0]; 
+0

好的。這是我提到的另一個問題。 我已經有了你建議的代碼,它的效果很好。 masterView位於UINavigationController裏面 – Arcadian 2010-11-04 13:19:54

+0

更新了答案。 – Anna 2010-11-04 13:31:36

+0

我想我做了這件事,並記住得到一個警告-customViewController可能不會響應doSomething – Arcadian 2010-11-04 13:45:42