如果您正在使用的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的類名。
我已更新了我的問題,並提供了有關我想要實現的更多信息的編輯。請告訴我。 – StuartM
好的,我克隆了這個回購,並且我已經加載了示例項目。如果您查看示例項目的故事板,您使用了哪種ViewController(比喻),以及您想要引用哪個ViewController? 這會讓你更容易回答你的問題。 – BigDan
謝謝,它很難解釋。假設我在屏幕上顯示了我的登錄VC。這意味着在堆棧順序中它將像這個InitalViewController(底部)一樣,它的topViewController屬性將是'WelcomeVC'(這是一個導航控制器),然後是Login View Controller顯示。如何訪問加載的初始視圖控制器的視圖屬性? (從屏幕上的登錄視圖控制器的imp文件中) – StuartM