Accessing a view controller created through Storyboard using the App Delegate

后端 未结 7 1738
佛祖请我去吃肉
佛祖请我去吃肉 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: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

提交回复
热议问题