xcode 文本字段应该返回,这对 ios7 是否正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21687778/
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
Text Field Should Return, is this correct for ios7?
提问by Esqarrouth
I have searched and seen a lot of answers about textFieldShouldReturn method. Half of them are outdated, some work in different cases, some are not explained thoroughly. I am not sure what is the most optimal way to do this task, it works but I think there might be an easier way to do it.
我搜索并看到了很多关于 textFieldShouldReturn 方法的答案。其中一半已经过时,有些在不同的情况下工作,有些没有得到彻底的解释。我不确定执行此任务的最佳方法是什么,它有效,但我认为可能有更简单的方法。
Heres my code so far:
到目前为止,这是我的代码:
On the header file:
在头文件上:
@interface ClassName : SomeUIViewController <UITextFieldDelegate>
In the implementation file:
在实现文件中:
@interface ClassName()
@property (weak, nonatomic) IBOutlet UITextField *firstOne;
@property (weak, nonatomic) IBOutlet UITextField *secondOne;
@property (weak, nonatomic) IBOutlet UITextField *thirdOne;
@property (weak, nonatomic) IBOutlet UITextField *fourthOne;
@property (weak, nonatomic) IBOutlet UITextField *fifthOne;
@end
-(void)viewDidLoad{
self.firstOne.delegate = self;
self.secondOne.delegate = self;
self.thirdOne.delegate = self;
self.fourthOne.delegate = self;
self.fifthOne.delegate = self;
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
if (textField == self.firstOne) {
[textField resignFirstResponder];
[self.secondOne becomeFirstResponder];
} else if (textField == self.secondOne) {
[textField resignFirstResponder];
[self.thirdOne becomeFirstResponder];
} else if (textField == self.thirdOne) {
[textField resignFirstResponder];
[self.fourthOne becomeFirstResponder];
} else if (textField == self.fourthOne) {
[textField resignFirstResponder];
[self.fifthOne becomeFirstResponder];
} else if (textField == self.fifthOne) {
[textField resignFirstResponder];
}
return NO;
}
So is this correct or are there better ways to do this task?
那么这是正确的还是有更好的方法来完成这项任务?
回答by zaph
In general your code is fine.
一般来说,你的代码很好。
If you are using IB (Interface Builder) you can set the delegate there instead of in code.
如果您使用的是 IB(接口生成器),您可以在那里而不是在代码中设置委托。
Example: control drag from the UITextField to the ViewController and select "delegate".
示例:控件从 UITextField 拖动到 ViewController 并选择“委托”。
The resignFirstResponder could be factored to ibe occurrence
Refactored code:
resignFirstResponder 可以被分解为 ibe 发生
重构的代码:
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
if (textField == self.firstOne) {
[self.secondOne becomeFirstResponder];
} else if (textField == self.secondOne) {
[self.thirdOne becomeFirstResponder];
} else if (textField == self.thirdOne) {
[self.fourthOne becomeFirstResponder];
} else if (textField == self.fourthOne) {
[self.fifthOne becomeFirstResponder];
}
return NO;
}
回答by Soto_iGhost
I propose you an other way to do this. You can create a new class, extending UITextField class with a category, and add your own property to navigate between UIControls.
我建议你另一种方式来做到这一点。您可以创建一个新类,使用类别扩展 UITextField 类,并添加您自己的属性以在 UIControl 之间导航。
// Class name UITextField+Navigation.h
@interface UITextField (Navigation)
@property (weak, nonatomic) IBOutlet id nextUIControl;
@end
The IBOutlet is for if you want to use the property with the storyboard, you can omit it if you want.
IBOutlet 用于如果您想在故事板中使用该属性,您可以根据需要省略它。
// UITextField+Navigation.m
#import "UITextField+Navigation.h"
#import <objc/runtime.h>
static char defaultHashKey;
@implementation UITextField (Navigation)
- (id)nextUIControl
{
return objc_getAssociatedObject(self, &defaultHashKey);
}
- (void)setNextUIControl:(id)nextUIControl
{
objc_setAssociatedObject(self, &defaultHashKey, nextUIControl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
In your class, import the extended UITextField+Navigation.h class so you can access the property you have created. In the viewDidLoad
method you just assign the value of the next UITextField
在您的类中,导入扩展的 UITextField+Navigation.h 类,以便您可以访问您创建的属性。在该viewDidLoad
方法中,您只需分配下一个 UITextField 的值
#import "UITextField+Navigation.h"
@interface ClassName()
@property (weak, nonatomic) IBOutlet UITextField *firstOne;
@property (weak, nonatomic) IBOutlet UITextField *secondOne;
@property (weak, nonatomic) IBOutlet UITextField *thirdOne;
@property (weak, nonatomic) IBOutlet UITextField *fourthOne;
@property (weak, nonatomic) IBOutlet UITextField *fifthOne;
@end
- (void)viewDidLoad
{
self.firstOne.nextUIControl = self.secondOne;
self.secondOne.nextUIControl = self.thirdOne;
etc...
}
Or if you are using Storyboards, you can link the controllers to the nextUIControl property.
And in the
textFieldShouldReturn:
或者,如果您使用的是 Storyboard,则可以将控制器链接到 nextUIControl 属性。
而在
textFieldShouldReturn:
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField.nextUIControl becomeFirstResponder];
return NO;
}
回答by Serge Maslyakov
My 50 cents. I use the next approach. UIKit objects (UIView, UITextField and other) have a rewritable property called "tag". It's a property for developer use.
我的50美分。我使用下一个方法。UIKit 对象(UIView、UITextField 和其他)有一个称为“标签”的可重写属性。它是供开发人员使用的属性。
typedef NS_ENUM (NSInteger, SSSActionType) {
TextFieldTag1= 100,
TextFieldTag2= 110,
TextFieldTag3= 120
};
- (void)customizeFields {
textField1.tag = TextFieldTag1;
textField1.delegate = self;
// setting tags for all fields
}
In delegate methods compare integer tags instead of instance objects:
在委托方法中比较整数标签而不是实例对象:
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
[textField resignFirstResponder];
switch (textField.tag) {
case TextFieldTag1:
// do stuff
break;
case TextFieldTag2:
break;
case TextFieldTag3:
break;
default:
break;
}
return NO;
}
Looks like the first approach but I prefer explicit tags name and clear code
看起来像第一种方法,但我更喜欢明确的标签名称和清晰的代码
回答by user2978613
as well as above good answers . you can still findout good library who manage all the stuffs of textfield . like when you are using textfields in tableview , textfields in collectionview . https://github.com/michaeltyson/TPKeyboardAvoiding. by using this library you can easily make login or sign up forms for app
以及上面的好答案。你仍然可以找到管理 textfield 所有东西的好图书馆。就像你在 tableview 中使用 textfields,在 collectionview 中使用 textfields 一样。https://github.com/michaeltyson/TPKeyboardAvoiding。通过使用这个库,您可以轻松地为应用程序登录或注册表格
回答by Yariv Nissim
My solution in Swift:
Create a dictionary mapping between a TextField and the next one.
我在 Swift 中的解决方案:
在 TextField 和下一个之间创建字典映射。
// to find the next text field
private lazy var textFields: [UITextField: UITextField] =
[self.firstTF: self.secondTF,
self.secondTF: self.thirdTF]
func textFieldShouldReturn(textField: UITextField) -> Bool {
textFields[textField]?.becomeFirstResponder()
return true
}
回答by xevser
I put every textfield a tag number consecutive. like 0,1,2,3..n then the usage is:
我将每个文本字段都放在一个连续的标签编号上。像 0,1,2,3..n 那么用法是:
func textFieldShouldReturn(textField: UITextField) -> Bool {
let tag = textField.tag
if (tag < n)
{
let textfieldNew = theView.viewWithTag(tag + 1) as! UITextField
textfieldNew.becomeFirstResponder()
}
else
{
textField.resignFirstResponder()
}
return true
}