如果 iPhone 应用程序中 UITextField 不为空,则启用 UIButton
在我的应用程序中,登录页面中有两个文本字段(用户名和密码)和 UI 按钮。
如果文本字段(密码)有文本,我想启用按钮。
我在 viewDidLoad() 中使用以下属性创建了 UITextfield 和 UIbutton。
self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.btnLogin setEnabled:NO];
我尝试过下面的代码,但没有运气。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.txtFieldPassword) {
// checking for any whitespaces
if(([string rangeOfString:@" "].location != NSNotFound))
{
return NO;
}
if( [[self.txtFieldPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] )
self.btnLogin.enabled = YES;
else
self.btnLogin.enabled = NO;
}
return YES;
}
但是,如果我删除了 UitextField 中的以下行,则它已经起作用了。
self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
我想启用具有上述属性的按钮。如何实现?
谢谢!!!
In my app, i have two textfields(username and Password) and UIbutton in SignIn page.
I want to enable button if textfield(password) having text.
I have created the UITextfield and UIbutton with below property in viewDidLoad().
self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.btnLogin setEnabled:NO];
I have tried the below code and there is no luck.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.txtFieldPassword) {
// checking for any whitespaces
if(([string rangeOfString:@" "].location != NSNotFound))
{
return NO;
}
if( [[self.txtFieldPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] )
self.btnLogin.enabled = YES;
else
self.btnLogin.enabled = NO;
}
return YES;
}
but, if i removed the below line in UitextField means, it have worked.
self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
i want to enable button with the above property. How to achieve it?
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只检查 string.length == 0 和 range.length > 0;这表示删除。然而,您真正想要的是只执行挂起的修改,然后检查是否仍然有一个非空字符串。
您可以通过 self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing; 来实现此目的财产。它对我来说工作得很好,祝你有美好的一天
You can just check for string.length == 0 and range.length > 0; this denotes a deletion. However what you really want is to just do the pending modification and then check that you still have a non-empty string.
you can achieve this with self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing; property. it is working fine for me, have a great day