开发者

how to get to appdelegate from viewcontrollers' value?

i know how to access appdelegate's value inside Viewcontroller like

 YourDelegate *appDe开发者_高级运维legate = (YourDelegate *)[[UIApplication sharedApplication] delegate];

but i want simple method like this when i want to get value from viewcontroller to appdelegate(reverse order).....? any help...?

suppose if i have one method in appdelate. i want to get data value from view controller page,i want to use it in appdelegte.m file.......?


To address this question more generally...If you want to do anything with an object – send it a message (call a method on an object), access some property of an object, pass the object as a parameter to some other method – you need a reference to that object.

That means that, in your case, your AppDelegate needs a reference to the view controller you want to access some property of. If the view controller is allocated and initialized in your app delegate, this is as simple as storing a reference to said view controller in your delegate until you need to use it (using an instance variable or whatever). If it wasn't, then you need to do something else to get your app delegate a reference to the view controller – the steps to do this would depend on where and how the view controller was created. Without more specific details, I can't help you with those steps.


Model-View-Controller (MVC) Sidenote:

If you are following MVC design practices, a view controller (or any other controller class) is not the object that should be storing your state information or other application data. That job is supposed to be performed by a model object.


Make the method be a class method (declared with + (void) MyMethod: (int)myParameter) and call it from your app delegate like this: [MyOtherViewController MyMethod: myParameter].


This to call checkAppTheme method from AppDelegate.m :

[(AppDelegate *)[[UIApplication sharedApplication] delegate] checkAppTheme];

Don't miss to change (checkAppTheme) to your method in your AppDelegate.m

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜