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 ?

  • one
    Not 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 1

 @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: in viewDidLoad , several objects of my class are created, in which there is a UIView type UIView , and in the same place I display them. In the touchBegan method touchBegan I want to understand which View 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