2013-02-20 139 views
3

这可能是一个简单的问题给某人。UITextField控制关闭按钮和键盘返回键

如何控制键盘的返回键。我有一个设置是这样的:

usernameField.returnKeyType = UIReturnKeyNext; 

其他这样的:

passwordField.returnKeyType = UIReturnKeyDone; 

我想对于每个不同的方法,但也可以通过if语句的textField.tag如果被控制必要。

其次,当点击UITextField中的关闭按钮时,是否有控制权(有一些代码运行)。我指的是文本字段中显示的光x,一旦字段中有文本,就会清除其数据的字段。或者一种不在场内有这个密封图标的方法?

回答

4

您可以检查该文本框,然后调用所需的相应的方法,

- (BOOL)textFieldShouldReturn:(UITextField *)textField //or implement - (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    if (textField == self.usernameField) { 
     [self callUserNameMethod]; 
    } else if (textField == self.passwordField) { 
     [self callPasswordMethod]; 
    } 

    [textField resignFirstResponder]; 
    return YES; 
} 

也可以提到使用Tag属性在你的问题。请注意,除了使用这些代表以外,没有其他方法可以在键盘上轻触默认返回按钮时设置方法。有notifications available for keyboard show and hide如果需要可以使用。

可以实现委托方法清除按钮为:

- (BOOL)textFieldShouldClear:(UITextField *)textField { 
    //write code to be executed on tap of clear button 
    return YES; 
} 

或者你可以设置clearButtonModeUITextFieldViewModeNever禁用它。

+1

太棒了,正是我需要的!谢谢 – StuartM 2013-02-21 11:37:57

2

为了回答你问题的第二部分,

usernameField.clearButtonMode = UITextFieldViewModeNever;