Reputation: 32258
I am extremely new to objective c development. I used a tutorial to learn how to add a label at run time, but I'm a little confused how to reference it later.
If I add...
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
myLabel.text = @"My Label";
[self.view addSubview:myLabel];
...in my viewDidLoad
method, how can I reference that label from another method on a separate occasion? Unlike C#, I don't name the label for later reference -- so I can't simply reference the name to set it.
Upvotes: 1
Views: 26194
Reputation: 11713
Upvotes: 1
Reputation: 135548
You have to declare an instance variable for the label in your class's @interface
(and optionally also a property for the ivar). Then assign the label to the ivar/property in viewDidLoad
and you can use it later in every other method of the class.
Don't forget to release
the label in your -dealloc
method. The code in your question leaks memory.
Upvotes: 2