ios 如何禁用/启用 UITextField 中的返回键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/788323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable/enable the return key in a UITextField?
提问by Sophie Alpert
Is there a way to programmatically enable or disable the Return Key on the UIKeyboard
? The closest I could find is enablesReturnKeyAutomatically
, but that only will tell whether to disable it at all.
有没有办法以编程方式启用或禁用 上的返回键UIKeyboard
?我能找到的最接近的是enablesReturnKeyAutomatically
,但这只能说明是否完全禁用它。
回答by Tharindu Madushanka
Maybe the following code segment helps:
也许以下代码段有帮助:
textfield.enablesReturnKeyAutomatically = YES;
This is publicly available in iPhone SDK in UITextInputTraits. Using this, return key will be disabled when no input text is available within text field.
这是在 UITextInputTraits 中的 iPhone SDK 中公开可用的。使用此功能,当文本字段中没有可用的输入文本时,将禁用返回键。
回答by Kyle Clegg
UITextField
's enablesReturnKeyAutomatically
property can be set right in Interface Builder, just select the textfield and open the Attributes inspector. As Tharindu stated, this will automatically enable and disable the return key depending on whether any text has been entered.
UITextField
的enablesReturnKeyAutomatically
属性可以在 Interface Builder 中设置,只需选择文本字段并打开属性检查器。正如 Tharindu 所说,这将根据是否输入任何文本自动启用和禁用返回键。
Of course, if you need to change this in code you can still set it programmatically using nameTextField.enablesReturnKeyAutomatically = true
.
当然,如果您需要在代码中更改此设置,您仍然可以使用nameTextField.enablesReturnKeyAutomatically = true
.
EDIT to address the downvotes:
编辑以解决downvotes:
Otherwise, there is no official way to enable and disable the return key on command. I would recommend against trying to use private APIs to accomplish this. Alternatively, you can use the textFieldShouldReturn:
delegate method and put your conditional/validation there and respond accordingly.
否则,没有官方的方法来启用和禁用命令上的返回键。我建议不要尝试使用私有 API 来实现这一点。或者,您可以使用textFieldShouldReturn:
委托方法并将您的条件/验证放在那里并做出相应的响应。
回答by Janne
You can override UITextField
's hasText
attribute to achieve this:
您可以覆盖UITextField
的hasText
属性来实现这一点:
class CustomTextField : UITextField {
override public var hasText: Bool {
get {
return evaluateString(text)
}
}
}
Where evaluateString(_ text: String?) -> Bool
checks against your needed input criteria, for example character count.
在哪里evaluateString(_ text: String?) -> Bool
检查您需要的输入标准,例如字符数。
Of course this does only work in combination with enablesReturnKeyAutomatically = true
set on the UITextField
.
当然,这只能与enablesReturnKeyAutomatically = true
.set 上的 set结合使用UITextField
。
I am aware that my answer is neither timely nor written in Objective-C, but given that I have not been able to find an answer anywhere else and this question being routinely referred to in other threads, I think that here is the best place to post it.
我知道我的回答既不及时,也不是用 Objective-C 写的,但鉴于我无法在其他任何地方找到答案,而且这个问题在其他线程中经常被提及,我认为这里是最好的地方发表它。
回答by SEQOY Development Team
One good idea is to create one file to access this class from anywhere. Here is the code:
一个好主意是创建一个文件以从任何地方访问此类。这是代码:
UIKeyboard.h
UIKeyboard.h
#import <UIKit/UIKit.h>
@interface UIApplication (KeyboardView)
- (UIView *)keyboardView;
@end
UIKeyboard.m
UIKeyboard.m
#import "UIKeyboard.h"
@implementation UIApplication (KeyboardView)
- (UIView *)keyboardView
{
NSArray *windows = [self windows];
for (UIWindow *window in [windows reverseObjectEnumerator])
{
for (UIView *view in [window subviews])
{
if (!strcmp(object_getClassName(view), "UIKeyboard"))
{
return view;
}
}
}
return nil;
}
@end
Now you can import and access this class from your own class:
现在你可以从你自己的类中导入和访问这个类:
#import "UIKeyboard.h"
// Keyboard Instance Pointer.
UIView *keyboardView = [[UIApplication sharedApplication] keyboardView];
A full documentation of this class you can find here:http://ericasadun.com/iPhoneDocs/_u_i_keyboard_8h-source.html
您可以在此处找到此类的完整文档:http : //ericasadun.com/iPhoneDocs/_u_i_keyboard_8h-source.html
More information you can find here:http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
您可以在此处找到更多信息:http : //cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
回答by Rik Renich
Here is a technique that is available from the documented API, but it does not provide visual feedback when the enter key is disabled.
这是一种可从记录的 API 中获得的技术,但在禁用 Enter 键时它不会提供视觉反馈。
- (void)setup {
// Or in init
self.textField.delegate = self;
}
// <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// substitute your test here
return [textField.text rangeOfString:@"@"].location != NSNotFound;
}
Other answers here can be used with
此处的其他答案可用于
[textField addTarget:self
action:@selector(validateTextField:)
forControlEvents:UIControlEventEditingChanged];
to provide dynamic visual feedback as the user types.
在用户键入时提供动态视觉反馈。
回答by Jean Luiz
Try to use a UITextField! to receive this string and than the return are gone!
尝试使用 UITextField!接收这个字符串,然后返回就消失了!