Question

I need to get in AppDelegate one parameter of some ViewController.

It not root for AppDelegate.

What is faster way to do it? Delegation?

Was it helpful?

Solution

Make it a property on your VC and then your AppDelegate can access it as needed.

OTHER TIPS

First something is terribly wrong with your design otherwise there shouldn't be any need for you to do something like this.

Second, you haven't provided any relevant information about your VC hierarchy and there is no general solution for this.

However , here are few workarounds / patches:

1) If you are using storyboard you can use :

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"mystoryboard"
                                              bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ExampleViewController"];

2) You can make make view controller singleton and access it directly from AppDelegate

3) Hacky Method: In AppDelegate have a @property (nonatomic, retain) UIVIewController *hackyViewController;

In hackyViewController.m do this

-(void)viewDidLoad{
    // call super

    YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    YourAppDelegate.hackyViewController =self;
}

Ideally , you should navigate through viewcontroller hierarchy using parentViewController and childViewcontroller property of UIVIewcontroller to get the instance. You can also make a recursive function which navigates through all childViewcontroller and check instance using iSKindOf to identify the viewcontroller you are looking for but this method does not work with all iOS configurations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top