2013-04-17 31 views
2

如何從一個控制器創建一個指針到另一個已經在堆棧上並由StoryBoard創建的控制器。獲取對當前堆棧中的視圖控制器的引用

例如,我使用方法instantiateViewControllerWithIdentifier將視圖控制器加載到堆棧/屏幕上。

我知道有另一個控制器背後,仍然加載稱爲InitialViewController(類)。我如何獲得一個參考/指向我知道在那裏的控制器的指針。

如果我嘗試從self/navigationController註銷presentViewController或presentsViewController,它們將返回null。 NSLog(@「present:%@」,self.navigationController.presentedViewController);

編輯 - 在應用上視圖控制器的更多信息

  1. 初始視圖控制器加載(子類的ECSlidingViewController
  2. 根據如果用戶在

    歡迎視圖控制器加載記錄(這是一個導航堆棧包括歡迎/登錄/註冊) 主頁視圖控制器加載(以家庭VC爲根的導航堆棧)

基本上,初始視圖控制器具有topViewController屬性,該屬性設置爲Home(導航堆棧)或Welcome(導航堆棧)。

初始視圖控制器總是在當前顯示屏上(從未見過)。

我該如何引用它,或者創建一個指向它的指針。例如,如果我在Login VC的imp文件中,我如何控制/鏈接到初始視圖控制器上的視圖,而無需使用alloc/init重新創建視圖?

回答

5

如果您正在使用的UITabBarController,你就可以用其子UIViewControllers引用:

[myTabBarController objectAtIndex:index]; 
NSLog(@"Selected view controller class type: %@, selected index: %d", [myTabBarController selectedViewController], [myTabBarController selectedIndex]); 

從零開始的索引方案如下選項卡的順序,你已對其進行設置,無論是編程或通過IB(最左邊的標籤=索引0)。因爲您似乎在搜索的UIViewController引用似乎是rootViewController(因爲您將其命名爲'InitialViewController'),所以您也可以在您的appDelegate中嘗試此操作。這將需要5秒:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions 
{ 

    NSLog(@"self.window.rootViewController = %@", NSStringFromClass([self.window.rootViewController class])); 

    UIViewController *myRootViewController = self.window.rootViewController; 

} 

讓我知道如果這樣做:)

無論您使用的是的UITabBarController UINavigationController的還是,我敢肯定那些人是你的RootViewController的把戲。一旦你搶參考吧,剩下的就是很簡單:

InitialViewController* myInitialViewController; 

for (UIViewController *vc in [myRootViewController childViewControllers]) { 
    if([vc isKindOfClass:[InitialViewController class]]){ 
     //Store this reference in a local/global variable or a property, 
     //or simply perform some logic on the vc pointer if you don't need to store it. 
     myInitialViewController = (InitialViewController *)vc; //Example & reminder to cast your reference 
    } 
} 

編輯基於查找出註釋中的新細節:

好吧,在你的topViewController的viewDidLoad或viewWillAppear中運行這段代碼:

//You have to import this class in order to reference it 
#import "MESHomeViewController.h" 

//Global variable for storing the reference (you can make this a property if you'd like) 
MESHomeViewController *myHomeVC; 

int i = 0; 
for (UIViewController *vc in [self.slidingViewController childViewControllers]) { 
    NSLog(@"Current vc at index %d = %@", i, [vc class]); 

    if ([vc isKindOfClass:[MESHomeViewController class]]) { 

     NSLog(@"Found MESHomeViewController instance - [[self.slidingViewController childViewControllers] objectAtIndex:%d]", i); 
     myHomeVC = vc; 
     break; 

    } 
    i++; 
} 

查看該參考文獻是否適用於您。如果是這樣,你的控制檯將打印出你的HomeViewController的類名。

+0

我已更新了我的問題,並提供了有關我想要實現的更多信息的編輯。請告訴我。 – StuartM

+0

好的,我克隆了這個回購,並且我已經加載了示例項目。如果您查看示例項目的故事板,您使用了哪種ViewController(比喻),以及您想要引用哪個ViewController? 這會讓你更容易回答你的問題。 – BigDan

+0

謝謝,它很難解釋。假設我在屏幕上顯示了我的登錄VC。這意味着在堆棧順序中它將像這個InitalViewController(底部)一樣,它的topViewController屬性將是'WelcomeVC'(這是一個導航控制器),然後是Login View Controller顯示。如何訪問加載的初始視圖控制器的視圖屬性? (從屏幕上的登錄視圖控制器的imp文件中) – StuartM

相關問題