新到快速:)
当一个注册按钮被按下时,我有一个MainView添加一个子视图(signUpWindow).
在我的signUpWindow子视图(SignUpWindowView.swift)中,我使用一个函数设置每个字段,例如:
func confirmPasswordText() { confirmPasswordTextField.frame=CGRectMake(50,210,410,50) confirmPasswordTextField.placeholder=("Confirm Password") confirmPasswordTextField.textColor=textFieldFontColor confirmPasswordTextField.secureTextEntry=true confirmPasswordTextField.returnKeyType = .Next confirmPasswordTextField.clearButtonMode = .WhileEditing confirmPasswordTextField.tag=5 self.addSubview(confirmPasswordTextField) }
我成功地让键盘在MainView中出现并消失时上下移动signUpWindow.好极了 :)
SignUpWindowView实现了UITextFieldDelegate
我的问题是我正在键盘上配置下一个/完成按钮,我不知道要添加textFieldShouldReturn函数的主视图(MainView或SignUpWindowView).我已经尝试过这两个,但是甚至不能得到一个println来测试,以查看该函数是否被执行.一旦我得到textFieldShouldReturn启动,我有信心我可以执行必要的代码,以获得下一个/完成按钮来做我想要的,并将发布最后的解决方案包括下一个/完成功能.
UPDATED包含一个缩写版本的SignUpWindowView.swift
import UIKit class SignUpWindowView: UIView,UITextFieldDelegate { let firstNameTextField:UITextField=UITextField() let lastNameTextField:UITextField=UITextField() override func drawRect(rect: CGRect){ func firstNameText(){ firstNameTextField.delegate=self firstNameTextField.frame=CGRectMake(50,25,200,50) firstNameTextField.placeholder="First Name" firstNameTextField.returnKeyType = .Next self.addSubview(firstNameTextField) } func lastNameText(){ lastNameTextField.delegate=self lastNameTextField.frame=CGRectMake(260,50) lastNameTextField.placeholder="Last Name" lastNameTextField.returnKeyType = .Done self.addSubview(lastNameTextField) } func textFieldShouldReturn(textField: UITextField!) -> Bool{ println("next button should work") if (textField === firstNameTextField) { firstNameTextField.resignFirstResponder() lastNameTextField.becomeFirstResponder() } return true } firstNameText() lastNameText() }
解决方法
您需要在类中实现UITextFieldDelegate,并将该对象设置为UITextField的委托.然后实现方法textFieldShouldReturn:像这样:
func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() if textField == someTextField { // Switch focus to other text field otherTextField.becomeFirstResponder() } return true }
在你的例子中,你错过了这一行:
confirmPasswordTextField.delegate = self
如果你已经执行了委托.