I\'m a noob in Objective-C and I have one question.
I have one UILabel
object that I adding to one UIView with this code:
UILabel *label = [
Example with UILabel
:
UILabel *label = (UILabel *)[self.view viewWithTag:1];
good luck!
You can get your subviews with for loop iteration
for (UIView *i in self.view.subviews){
if([i isKindOfClass:[UILabel class]]){
UILabel *newLbl = (UILabel *)i;
if(newLbl.tag == 1){
/// Write your code
}
}
}
Swift
let label:UILabel = self.view.viewWithTag(1) as! UILabel
Swift 3.0 and Swift 4.0
if let subLabel:UILabel = primaryView.viewWithTag(123) as? UILabel {
subLabel.text = "do things here :-D"
}
You can get the subview with the code which others have mentioned, just like
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
But an important point to remember,
viewWithTag:
method will return the receiver view (on which you are calling viewWithTag:
) instead of returning the actual subview you want.So keep the parent view and child views tags distinct whenever you need to use viewWithTag:
.
If you are on the same view
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
Also, if you want a new instance of UILabel
UILabel *newTagLabel = [tagLabel copy];
//customize new label here...
[view addSubView:newTagLabel];
You could use the viewWithTag: method.