Iphone access a property value from AppDelegate

后端 未结 3 1216
渐次进展
渐次进展 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.

提交回复
热议问题