iphone: uitextfield, multiple textfields with the same delegate?

后端 未结 2 2004
清酒与你
清酒与你 2021-01-18 05:17

there are two different textfields in my applications and i set the .delegate property of both of them to: self. Now i implemented different methods from the uitextfielddele

相关标签:
2条回答
  • 2021-01-18 06:07
    UITextField *textFieldOne=..... 
    
    UITextField *textFieldTwo=....
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(textField == textFieldOne)
    { // field one code
    }else{
    //field two code
    }
    }
    

    have two references of the inserted text views and u can compare them at the delegate methods. Not much needed with tags

    0 讨论(0)
  • 2021-01-18 06:09

    Set a tag on the textfield on initialization, then check the UITextField object that is passed into the delegate method's tag, then you'll be able to make a differentiation between the two textfields:

    #define FIELD_ONE_TAG 1
    #define FIELD_TWO_TAG 2
    
    UITextField *textFieldOne = ...
    textFieldOne.tag = FIELD_ONE_TAG;
    ...
    UITextField *textFieldTwo = ...
    textFieldTwo.tag = FIELD_TWO_TAG;
    
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
       if(textField.tag == FIELD_ONE_TAG) { //field one
       } else {//field two
    
       }
    }
    
    0 讨论(0)
提交回复
热议问题