在文本字段中插入一个字符后启用完成按钮:textFieldDidEndEditing:或textFieldShouldBeginEditing:或?
当用户在 uitextfield 中至少写入一个字符时,我想启用导航栏上的完成按钮(在模式视图中)。我尝试过:
- textFieldDidEndEditing:当前一个 uitextfield 退出第一响应者时启用该按钮(因此当前 uitextfield 中的零个字符)。
- textFieldShouldBeginEditing:当文本字段成为第一响应者时调用。 还有其他方法可以做到这一点吗?
[编辑]
解决方案可能
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
既不可行
[self.navigationItem.rightBarButtonItem setEnabled:YES];
,也不
[doneButton setEnabled:YES]; //doneButton is an IBOutlet tied to my Done UIBarButtonItem in IB
可行。
I would like to enable the done button on the navbar (in a modal view) when the user writes at least a char in a uitextfield. I tried:
- textFieldDidEndEditing: enables the button when the previous uitextfield resigns first responder (so with the zero chars in the current uitextfield).
- textFieldShouldBeginEditing: is called when the textfield becomes the first responder.
Is there another way to do this?
[EDIT]
The solution could be
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
but neither
[self.navigationItem.rightBarButtonItem setEnabled:YES];
or
[doneButton setEnabled:YES]; //doneButton is an IBOutlet tied to my Done UIBarButtonItem in IB
work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正确的代码是;
编辑:
正如 MasterBeta 之前和 David Lari 后来正确指出的那样,该事件应该响应 Editing Changed。我正在用 David Lari 的解决方案更新答案,因为这是标记为正确的解决方案。
The correct code is;
Edit:
As correctly pointed out by MasterBeta before and David Lari later, the event should respond to Editing Changed. I'm updating the answer with David Lari's solution as this was the one marked as correct.
但是当用户按下文本字段控件的清除按钮时,不会调用
shouldChangeCharactersInRange
。当文本字段为空时,您的按钮也应该被禁用。IBAction
可以与文本字段控件的Editing Changed 事件连接。当用户键入或按清除按钮时将调用它。But
shouldChangeCharactersInRange
won't be called when user press clear button of text field control. And your button should also be disabled when text field is empty.A
IBAction
can be connected with Editing Changed event of text field control. And it will be called when users type or press clear button.@MasterBeta:几乎是正确的。按照他的说明将操作连接到编辑已更改,但此代码更简单并且没有拼写错误:
@MasterBeta: Almost correct. Follow his instructions to connect an action to Editing Changed, but this code is simpler and has no typos:
实际上,在 Xcode 6.x 中标记 ON Auto-enable Return Key 就足够了
Actually, in Xcode 6.x is sufficient to flag in ON Auto-enable Return Key
这个答案似乎适用于所有场景。单一字符,清晰且变化万千。希望有人觉得这有帮助。
This answer seems to be working in all scenarios. Single character, clear and all changes. Hope someone finds this helpful.
Swift 2.2
您可以将自定义方法“checkTextField()”分配给“myTextField”UITextField,如下所示:
并将方法内的完成按钮切换为:
不需要任何委托。
Swift 2.2
You can assign custom method "checkTextField()" to "myTextField" UITextField as:
and toggle the done button inside the method as:
No need of any delegate.
尝试并继续:
}
这个答案被标记为正确,而实际上下面的“w4nderlust”存在更好的解决方案。这个答案是他们的,功劳就归他们吧!
try and go with:
}
This answer was marked correct when infact a better solution existed by 'w4nderlust' below. This answer is theirs, let them take the credit!