开发者

How do I identify or know which control is being referenced in a method?

开发者 https://www.devze.com 2022-12-31 18:36 出处:网络
In my specific case, how 开发者_运维问答do I identify or know which UITextField is being referenced in shouldChangeCharactersInRange 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?


  1. 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 compare textField 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

0

精彩评论

暂无评论...
验证码 换一张
取 消