Created own class. An object of this own class is created in the viewDidLoad
method. How to correctly access this object from another method, for example, from touchBegin
?
- oneNot strong in objective-c, but I suppose, as in any OOP, you can assign an instance of an object to a variable / field / property of your own class. This variable will be available within the entire class, in any method. - SlyDeath
|
1 answer
@interface ViewController : UIViewController @property (nonatomic, strong) MyClass* myObject; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.myObject = [MyClass new]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.myObject = nil;// или что угодно } @end
- Thank! Did I understand correctly that adding
@property
to the class in which I want to see my object is the only correct way? Initially, the task was this: inviewDidLoad
, several objects of my class are created, in which there is aUIView
typeUIView
, and in the same place I display them. In thetouchBegan
methodtouchBegan
I want to understand whichView
I touched. Is it really necessary for me to produce@property
? - Sergey Gamayunov - or property or class variables, if you have several objects, you can throw them into an array and make the property array itself - Andrey Chernukha
|