开发者

Where to create instances

开发者 https://www.devze.com 2023-02-28 13:46 出处:网络
I am unclear as to where I should create my objects. If I create them in viewDidLoad in my ViewController, obviously they can\'t be accessed outside that method. For example, when a UIButton is touche

I am unclear as to where I should create my objects. If I create them in viewDidLoad in my ViewController, obviously they can't be accessed outside that method. For example, when a UIButton is touched in a View, I want my objects to be modified. But if the objects were created in viewDidLoad (for example), the IBAction that fires开发者_如何学JAVA when a UIButton is touched doesn't know about my object.

I'm pretty obviously missing something very fundamental and would appreciate some pointers.


You can create them in viewDidLoad, but to make them accessable in your instance you'll have to make them ivars(instance variables) or properties.

You can read more about Properties in Objective-C 2.0 on cocoacast.com.

I recommend Apple's official book The Objective-C Programming Language:

  • Class interface
  • Referring to Instance Variables
  • Declared Properties - Overview
  • Using Properties


Declare that UIButton in the .h file... then you can able to access the button anywhere...


if you want to access the state any where in the code you just declare that as state(global declaration ) which should be in the .h file.The state can be alloc in the viewdidload or any method according to usage.

0

精彩评论

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