2

如何创建从一个控制器到另一个控制器的指针,该控制器已经在堆栈上并由 StoryBoard 创建。

例如,我使用该方法instantiateViewControllerWithIdentifier将视图控制器加载到堆栈/屏幕上。

我知道这背后还有另一个控制器,仍然加载称为 InitialViewController(类)。如何获得我知道的控制器的引用/指针。

如果我尝试从 self/navigationController 注销presentingViewController 或presentingViewController,它们会返回null。NSLog(@"presented: %@",self.navigationController.presentedViewController);

编辑 - 有关应用程序中视图控制器的更多信息

  1. 加载的初始视图控制器( ECSlidingViewController的子类)
  2. 取决于用户是否登录

    已加载欢迎视图控制器(这是一个导航堆栈,包括欢迎/登录/注册) 已加载主视图控制器(以家庭 VC 为根的导航堆栈)

基本上,初始视图控制器具有 topViewController 的属性,该属性设置为 Home(导航堆栈)或 Welcome(导航堆栈)。

初始视图控制器始终在当前显示器上(只是从未见过)。

我如何引用它,或创建指向它的指针。例如,如果我在 Login VC 的 imp 文件中,我如何控制/链接到初始视图控制器上的视图而不用 alloc/init 重新创建它?

4

1 回答 1

5

If you are using UITabBarController, you can get references to its child UIViewControllers via:

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

The zero-based indexing scheme follows the order of the tabs as you've set them up, whether programmatically or through IB (leftmost tab = index 0).

Because the UIViewController reference you appear to be searching for seems to be the rootViewController (because of the way you named it; 'InitialViewController'), you can also try this in your appDelegate. This will take 5 seconds:

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

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

    UIViewController *myRootViewController = self.window.rootViewController;

}

Let me know if that does the trick :)

Whether you're using a UITabBarController or UINavigationController, I'm certain one of those is your rootViewController. Once you grab the reference to it, the rest is pretty easy:

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
    }
}

EDIT BASED ON NEW DETAILS FOUND IN COMMENTS:

Ok, run this code in your topViewController's viewDidLoad or 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++;
}

See if the reference is available to you there. If it is, your console will print out the class name of your HomeViewController.

于 2013-04-17T16:28:53.453 回答