具有相同委托的多个文本字段

具有相同委托的多个文本字段

本文介绍了iPhone:uitextfield,具有相同委托的多个文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两个不同的文本字段,我将它们的.delegate属性都设置为:self。
现在我实现了与uitextfielddelegate协议不同的方法,但是我希望能够分别控制两个文本字段。例如,我希望第一个文本字段在编辑开始时做的事情与第二个文本字段有所不同...是此问题的唯一解决方案,是将分配设置为不同的委托,或者有一种方法可以使两个文本字段都具有分配给他们的是同一位代表?
我希望我的问题是可以理解的,我试图用它来解释我可以使用的最佳方式.....
预先感谢!

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 uitextfielddelegate protocol but i want to be able to control the two textfields individually. For instance i want the first text field to do something different when editing begins than the second textfield... Is the only solution to this problem to set the assign a different delegate or is there a way to do this with both of the textfield having the same delegate assigned to them?I hope my question is understandable i tried it to explain the best way that i could.....thanks in advance!

推荐答案

在初始化时在文本字段上设置标记,然后检查 UITextField 对象,传递到委托方法的标记中,则可以区分两个文本字段:

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

   }
}

这篇关于iPhone:uitextfield,具有相同委托的多个文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:19