I want to call ViewController
's method in AppDelegate.m.
i have method method() in my Viewcontroller
.i want it to be called when didSelectMethod() called in appdelegate.m
.
I have called method like this.-
ViewController *vc=[[ViewController alloc]init];
[vc method];
method is called but not having same instance as actually method having.
it having all nill
values. can anyone give me right code for this what i want.
Thank you Jagveer Rana
First you have declare your ViewController Class in Appdelegate.h and make an object of your UIViewController class in AppDelegate.h ,like this
Now import your ViewController class in AppDelegate.m, like this
Now in AppDeledate.m 's method applicationDidFinishLaunchingOption: create a new object of your viewController and assign it to obj1 ,like this
With help of this code you can parse data between viewcontrollers or objects..Now in your ViewController Class's .m file you have to import Appdelegate.h and parse all data from your object of viewController to obj1 and obj1 will parse that data to obj2 (with the helo of above code).[Assuming object of your viewController class as OBJ].
NOTE- i haven't tested this code..please save your project first somewhere else..answered according to my knowledge..hope this will help you..Thank you
While this has been correctly answered for the case where the view controller is the
rootViewController
, for completeness sake here's how you can get this to work with any view controller:The simplest way to get the same instance of anyViewController in your app is track it one by one... Like from any viewController in your app
or from appDelagate
it will give you the rootViewController then from this rootViewController track your desired viewController.
In the .pch file write this code
In Any ViewController you want to call Delegate's method then simply write this code.
In your question you create a new view controller instance which is not part of the view hierarchy so you won't see any effect by calling the method (UI-wise). (Also it was not initialized via xib/storyboard so your UI elements will probably be nil)
You can access it via the window as long as it is the root view controller (otherwise you will have to find it or keep a reference to it when created):