xcode 如何使用 Swift iOS 监听 UIReturnKeyType.Next
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24187860/
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 listen for UIReturnKeyType.Next with Swift iOS
提问by Stephen Fox
When the user presses next on the keypad I want to move from the current UITextField to the next UITextField, the UIReturnKeyType is set to UIReturnKeyType.Next
当用户在键盘上按下下一步时,我想从当前 UITextField 移动到下一个 UITextField,UIReturnKeyType 设置为 UIReturnKeyType.Next
Here is how I have the UITextField set up.
这是我设置 UITextField 的方法。
username.returnKeyType = UIReturnKeyType.Next
username.textColor = UIColor.whiteColor()
username.placeholder = "Username"
username.borderStyle = UITextBorderStyle.RoundedRect
uUsername.textAlignment = NSTextAlignment.Center
username.backgroundColor = UIColor.lightGrayColor()
username.font = UIFont(name: "Avenir Next", size: 14)
username.autocapitalizationType = UITextAutocapitalizationType.None
username.autocorrectionType = UITextAutocorrectionType.No
回答by Mick MacCallum
You have to configure the UITextFieldDelegate method textFieldShouldReturn:
. From within this method, you can check which text field is returning and reassign first responder status accordingly. Of course this mean you'll have to assign your class as the delegate of the text fields.
您必须配置 UITextFieldDelegate 方法textFieldShouldReturn:
。在此方法中,您可以检查哪个文本字段正在返回并相应地重新分配第一响应者状态。当然,这意味着您必须将您的班级分配为文本字段的代表。
Example:
例子:
class MyClass: UITextFieldDelegate {
func setup() { // meaningless method name
textField1.delegate = self
textField2.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if (textField === textField1) {
textField2.becomeFirstResponder()
} else if (textField === textField2) {
textField2.resignFirstResponder()
} else {
// etc
}
return true
}
}
回答by Manish Verma
Follow the steps given below:
请按照以下步骤操作:
1) You will be needing to set the tags in the incremental sequence of the textfields in xib/Storyboard or in code.
1) 您将需要在 xib/Storyboard 或代码中以文本字段的增量序列设置标签。
2) Set the delegate for each of the textfields.
2) 为每个文本字段设置委托。
3) Then paste the following code in your view controller to have the desired effect:
3)然后将以下代码粘贴到您的视图控制器中以获得所需的效果:
func textFieldShouldReturn(textField: UITextField) -> Bool
{
let nextTag: Int = textField.tag + 1
let nextResponder: UIResponder? = textField.superview?.superview?.viewWithTag(nextTag)
if let nextR = nextResponder
{
// Found next responder, so set it.
nextR.becomeFirstResponder()
}
else
{
// Not found, so remove keyboard.
textField.resignFirstResponder()
}
return false
}
Hope this helps!!
希望这可以帮助!!
回答by jmcastel
If you have two textfield : - textField1 - textField2
如果您有两个文本字段: - textField1 - textField2
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField1.resignFirstResponder()
textField2.becomeFirstResponder()
return true
}
回答by jayesh kavathiya
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
@IBOutlet var txtValue: UITextField //create a textfield variable
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self //set delegate to textfile
}
func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method
return true
}