如果 iPhone 应用程序中 UITextField 不为空,则启用 UIButton

发布于 2024-12-25 12:48:18 字数 1033 浏览 2 评论 0原文

在我的应用程序中,登录页面中有两个文本字段(用户名和密码)和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2025-01-01 12:48:18

您可以只检查 string.length == 0 和 range.length > 0;这表示删除。然而,您真正想要的是只执行挂起的修改,然后检查是否仍然有一个非空字符串。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *testString = [textField.text stringByReplacingCharactersInRange:range withString:string];
testString = [testString stringByTrimmingCharactersInSet:
              [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if( testString.length)
    myButton.enabled = YES;
else
    myButton.enabled = NO;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
 _done_button.enabled = NO;
  return YES;
}

您可以通过 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.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *testString = [textField.text stringByReplacingCharactersInRange:range withString:string];
testString = [testString stringByTrimmingCharactersInSet:
              [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if( testString.length)
    myButton.enabled = YES;
else
    myButton.enabled = NO;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
 _done_button.enabled = NO;
  return YES;
}

you can achieve this with self.txtFieldPassword.clearButtonMode = UITextFieldViewModeWhileEditing; property. it is working fine for me, have a great day

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文