How do I identify or know which control is being referenced in a method?
In my specific case, how 开发者_运维问答do I identify or know which UITextField is being referenced in shouldChangeCharactersInRange method?
I know the parameter (UITextField*)textField contains the object being referenced, but how do I compare to identify which one it is?
If you create your interface using IB then you can create
IBOutlet
in your controller for each UI element, connect then in IB and later comparetextField
parameters with them://header IBOutlet UITextField* nameField; IBOutlet UITextField* addressField; //Implementation ... if (textField == nameField){ } if (textField == addressField){ }
2 In IB you can also assign a unique tag value for each UITextField
(available for each UIView subclasses) and compare tag values:
#define nameTag 10
#define addressTag 11
//Implementation
...
if (textField.tag == nameTag){
}
if (textField.tag == addressTag){
}
The most elegant solution is to use tags from interface builder / storyboard, assign tags for each of the text fields.
Then use a switch(textfield.tag) case in your code, code looks much cleaner compared to putting a lot of if statements
精彩评论