UITextField secureTextEntry - 从 YES 变为 NO,但改回 YES 无效 [英] UITextField secureTextEntry - works going from YES to NO, but changing back to YES has no effect
问题描述
以上说明了一切 - 我有一个 UITextField 设置为安全,但想让用户选择使其不安全(这样他们就可以确定他们在私人区域输入了什么).但是,假设他们误按了切换键,并想将其改回安全模式?那行不通.我已经尝试了一切 - 使用 -1 而不是 YES,删除文本然后将其放回原处.我对其他想法完全不知所措.[输入rdar://9781908]
The above says it all- I have a UITextField set to secure, but want to give users the option to make it not secure (so they can see for sure what they typed if they are in a private area). However, suppose they hit the toggle by mistake, and want to change it back to secure mode? That does not work. I've tried everything - using -1 instead of YES, deleting the text and then putting it back. I'm at a total loss on other ideas. [Entered rdar://9781908]
我相信这个问题从 iOS5 开始就已经解决了.
I believe this issue is fixed as of iOS5.
推荐答案
必定是 input-focus 问题:当获得焦点时,UITextField 只能改变 ON->OFF.
尝试下一个技巧来关闭->打开:
It must be input-focus issue: when focused, UITextField can change only ON->OFF.
Try next trick to switch OFF->ON:
textField.enabled = NO;
textField.secureTextEntry = YES;
textField.enabled = YES;
[textField becomeFirstResponder];
EDIT (dhoerl):在 iOS 9.3 中,我发现这可行,但存在问题.如果您在普通视图中输入 3 个字符,然后切换到安全文本,然后键入一个字符,则 3 个预先存在的字符将消失.我尝试了各种技巧来清除,然后重置文本没有成功.我终于尝试通过剪切和粘贴来玩控制 - 粘贴到新切换到安全模式的效果很好.所以我在代码中模拟了它,现在一切正常 - 也不需要与响应者一起玩.这是我最终得到的结果:
EDIT (dhoerl): In iOS 9.3, I found this works but there is a problem. If you enter say 3 characters in plain view, then switch to secure text, then type a character, the 3 pre-existing characters disappear. I tried all kinds of trick to clear, then reset the text without success. I finally tried just playing with the control by cutting and pasting - pasting into the newly-switched-to-secure-mode worked great. So I emulated it in code, and now it all works fine - no need to play with the responder either. Here is what I finally ended up with:
if textview should go into secure mode and the textfield is not empty {
textField.text = ""
textField.secureTextEntry = true
UIPasteboard.generalPasteboard().string = password
textField.paste(self)
UIPasteboard.generalPasteboard().string = ""
}
我不想做这个粘贴,但如果你想要它无缝,这是我能找到的唯一方法.
I'd prefer to not have to do this paste, but if you want it seamless this is the only way I can find to do it.
这篇关于UITextField secureTextEntry - 从 YES 变为 NO,但改回 YES 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!