Home > Net >  Swift: programmatically locate a UITextFelds UILabel and containing UIView container
Swift: programmatically locate a UITextFelds UILabel and containing UIView container

Time:07-25

I have a form with a UIView wrapping around a UILabel and UITextField. When a user enters the field I would like to change the colour of the label and the border colour of the view container.

If I call a function on the firstResponder I will need to find the text field's corresponding label and view copntainer.

I thought to have a firstResonder function for each field and in each function send the corresponding outlets (textfield, label, view) to a function which handles the colour changes for the label and view border.

This is not terrible but I and sure this can be accomplished more efficiently.

Any pointers please.

CodePudding user response:

The usual thing is to give each member of each group a corresponding tag. Since viewWithTag drills down to find any view with the given tag, the problem is solved if you know how to convert the tag value of the view you have to the tag value of the view you want.

For example, text field 10, view 110, label 210; text field 11, view 111, label 211; and so on. Or whatever system suits your fancy.

Alternatively just walk the view hierarchy. The view is the text field's superview, and the label is the first subview of the view that is a label.

  • Related