Accessing a view controller created through Storyboard using the App Delegate

后端 未结 7 1714
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 10:41

I\'m working on an iOS5 app using storyboard, and I have a method in a view controller class that i\'d like to access from the App Delegate. The trouble is, this view contr

相关标签:
7条回答
  • 2020-12-09 11:24

    I have a better solution. You can use window.rootViewController.storyboard to get a reference to the storyboard of that view.

    0 讨论(0)
  • 2020-12-09 11:28

    Swift

    This is the Swift version of @infiniteLoop's answer.

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myViewController = storyboard.instantiateViewControllerWithIdentifier("identifier") as! MyViewController
    

    You need to replace the storyboard name, view controller name, and view controller identifier with whatever you used in your project.

    Related tasks

    • Pass data to the view controller
    • Present and dismiss a view controller modally
    0 讨论(0)
  • 2020-12-09 11:34

    You'll have to traverse the view hierarchy from the app delegate. Assuming the AppDelegate holds a reference to the UITabBarController, you could use the viewControllers property or selectedViewController property to get to your view controller.

    0 讨论(0)
  • 2020-12-09 11:35

    Thanks to Jerry (above), here's the code that got me what I wanted:

        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    MasterViewController *result;
    
    //check to see if navbar "get" worked
    if (navigationController.viewControllers) 
    
        //look for the nav controller in tab bar views 
        for (UINavigationController *view in navigationController.viewControllers) {
    
            //when found, do the same thing to find the MasterViewController under the nav controller
            if ([view isKindOfClass:[UINavigationController class]])
                for (UIViewController *view2 in view.viewControllers) 
                    if ([view2 isKindOfClass:[MasterViewController class]])                    
                        result = (MasterViewController *) view2;
    }
    
    0 讨论(0)
  • 2020-12-09 11:39

    I probably would do it in reverse. Create a method in AppDelegate call registerViewController or something. In every ViewController, call the method with itself with a name. Then in AppDelegate, you can do whatever you want afterward. It is messy to do all sort of drill

    In AppDelegate

    - (void)registerViewController:(NSString *)name controller:(UIViewController *)controller
    {
        if(_viewControllers == nil)
        {
            _viewControllers = [[NSMutableDictionary alloc] init];
    
        }
    
        [_viewControllers setObject:controller forKey:name];
    }
    

    In any ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        [appDelegate registerViewController:@"feelings" controller:self];
    }
    

    In AppDelegate

    XViewController *vc = (XViewController *)[_viewControllers objectForKey:@"X"];
    [X startSpinner];
    

    Hope this help

    BS

    0 讨论(0)
  • 2020-12-09 11:43

    In the AppDelegate, you could simply use indexes to access the controllers via UITabBarController:

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    firstViewController = [[tabBarController viewControllers] objectAtIndex:0];
    secondViewController = [[tabBarController viewControllers] objectAtIndex:1];
    thirdViewController = [[tabBarController viewControllers] objectAtIndex:2];
    

    Naturally, this will get messed up if you change the ordering of the tab bar. Also, if you're looking for a controller deeper in the hierarchy, you need to do a bit more legwork.

    0 讨论(0)
提交回复
热议问题