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.
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 inviewDidLoadand you can use it later in every other method of the class.Don’t forget to
releasethe label in your-deallocmethod. The code in your question leaks memory.