TazmanNZL TazmanNZL - 1 year ago 123
iOS Question

Get UILabel from UIView

I have added a number of labels to a view giving them tags.

What is the correct way to retrieve a label from the view. I am wanting to re-postion the label. Here is what I am using to retrieve the label and re-position it:

UILabel *theLabel = (UILabel *)[self.view viewWithTag:5];
[theLabel.layer setPosition:CGPointMake(100, 200)];

Is this the correct way of doing it?

Answer Source

To retrieve the label, use an IBOutlet if you've created it in Interface Builder, or a raw property or ivar if created in code. There's no need to loop the subviews or use viewWithTag if you've got a direct reference (outlet, property, ivar) to it.

To move the label, directly set its frame or center property without needing to access its layer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download