Iphone access a property value from AppDelegate

后端 未结 3 1208
渐次进展
渐次进展 2020-12-24 12:31

How to access a property value of AppDelegate class from someView Controller without creating reference of the delegate in view controller?

相关标签:
3条回答
  • 2020-12-24 13:14

    I'm not quite sure what you mean - there are multiple ways to get information from your application delegate into a view controller, and the phrase "without creating reference of the delegate" is unclear. Your options basically are:

    1. Reference the application delegate, casting as appropriate. You would write code in your view controller class like:
      id propertyValue = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myProperty];
    2. Pass the property in when creating the view controller. This requires the view controller to have a @property declared and @synthesized for use, then you would have the app delegate just set the property on the view controller instance.

    Neither of these options require that you retain a copy of your app's delegate as a @property, but the first does reference the delegate once.

    0 讨论(0)
  • 2020-12-24 13:19
    [UIApplication sharedApplication].delegate;
    
    0 讨论(0)
  • 2020-12-24 13:21

    [UIApplication sharedApplication].delegate

    You'll also need to include the app delegate header file in your view controller and possibly typecast the delegate from id to your actual app delegate class.

    #include "MyAppDelegate.h"
    
    ((MyAppDelegate *)[UIApplication sharedApplication].delegate).myProperty;
    
    0 讨论(0)
提交回复
热议问题