如何比较UITextField的实例

xue*_*eru 2 iphone

我在一个视图上有3个UITextFileds,我想在UITextFieldDelegate方法中只在其中两个中应用逻辑,我如何确定触发回调的UITextField?

提前谢谢了!

ken*_*ytm 7

通常,简单的指针比较有效,因为您只想检查对象标识.

-(BOOL)textFieldShouldReturn:(UITextField*)textField {
   if (textField != theIgnoredTextField) {
      ...
Run Code Online (Sandbox Code Playgroud)

或者,您可以将.tags 分配给文本字段.

-(BOOL)textFieldShouldReturn:(UITextField*)textField {
   if (textField.tag != 37) {
      ...
Run Code Online (Sandbox Code Playgroud)

优点是您不需要存储引用theIgnoredTextField,并且可以从Interface Builder设置标记,但它依赖于识别幻数"37".