下一步/完成button使用Swift与textFieldShouldReturn

我有一个MainView,当注册button被按下时,添加一个子视图(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) } 

我有键盘移动signUpWindow上下时,它出现在MainView中消失。

SignUpWindowView实现了UITextFieldDelegate

我的问题是,我正试图configuration键盘上的下一步/完成button,并不确定哪个视图( MainViewSignUpWindowView )添加textFieldShouldReturn函数。 我已经尝试了两个,但是甚至不能得到一个println来testing,看看该函数是否被执行。 一旦我得到textFieldShouldReturn火,我相信我可以执行必要的代码来获得下一个/完成button来做我想做的,并将发布最终的解决scheme,包括下一个/完成function。

已更新包含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, 25, 200, 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 

如果你已经实施了委托,当然。

我试图在SignUpWindowView.swift中testing我的文本字段,这是所有textFields创build的地方。 但是,因为我把SignUpWindowView作为子视图放到了我的MainViewController中,所有的UITextField“处理”都需要在MainView中完成,而不是在子视图中完成。

所以这里是我的整个代码(在这一刻)为我的MainViewController,它处理移动我的SignUpWindowView向上/向下键盘显示/隐藏,然后从一个字段移动到下一个。 当用户在最后一个文本字段(键盘的下一个button现在被设置为在子视图中完成)时,键盘会消失,用户可以通过注册button提交表单。

MainViewController:

 import UIKit @objc protocol ViewControllerDelegate { func keyboardWillShowWithSize(size:CGSize, andDuration duration:NSTimeInterval) func keyboardWillHideWithSize(size:CGSize,andDuration duration:NSTimeInterval) } class ViewController: UIViewController,UITextFieldDelegate { var keyboardDelegate:ViewControllerDelegate? let signUpWindow=SignUpWindowView() let signUpWindowPosition:CGPoint=CGPointMake(505, 285) override func viewDidLoad() { super.viewDidLoad() // Keyboard Notifications NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) // set the textFieldDelegates signUpWindow.firstNameTextField.delegate=self signUpWindow.lastNameTextField.delegate=self signUpWindow.userNameTextField.delegate=self signUpWindow.passwordTextField.delegate=self signUpWindow.confirmPasswordTextField.delegate=self signUpWindow.emailTextField.delegate=self } func keyboardWillShow(notification: NSNotification) { var info:NSDictionary = notification.userInfo! let keyboardFrame = info[UIKeyboardFrameEndUserInfoKey] as! NSValue let keyboardSize = keyboardFrame.CGRectValue().size var keyboardHeight:CGFloat = keyboardSize.height let animationDurationValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber var animationDuration : NSTimeInterval = animationDurationValue.doubleValue self.keyboardDelegate?.keyboardWillShowWithSize(keyboardSize, andDuration: animationDuration) // push up the signUpWindow UIView.animateWithDuration(animationDuration, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { self.signUpWindow.frame = CGRectMake(self.signUpWindowPosition.x, (self.signUpWindowPosition.y - keyboardHeight+140), self.signUpWindow.bounds.width, self.signUpWindow.bounds.height) }, completion: nil) } func keyboardWillHide(notification: NSNotification) { var info:NSDictionary = notification.userInfo! let keyboardFrame = info[UIKeyboardFrameEndUserInfoKey] as! NSValue let keyboardSize = keyboardFrame.CGRectValue().size var keyboardHeight:CGFloat = keyboardSize.height let animationDurationValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber var animationDuration : NSTimeInterval = animationDurationValue.doubleValue self.keyboardDelegate?.keyboardWillHideWithSize(keyboardSize, andDuration: animationDuration) // pull signUpWindow back to its original position UIView.animateWithDuration(animationDuration, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { self.signUpWindow.frame = CGRectMake(self.signUpWindowPosition.x, self.signUpWindowPosition.y, self.signUpWindow.bounds.width, self.signUpWindow.bounds.height) }, completion: nil) } func textFieldShouldReturn(textField: UITextField) -> Bool { switch textField { case signUpWindow.firstNameTextField: signUpWindow.lastNameTextField.becomeFirstResponder() break case signUpWindow.lastNameTextField: signUpWindow.userNameTextField.becomeFirstResponder() break case signUpWindow.userNameTextField: signUpWindow.passwordTextField.becomeFirstResponder() break case signUpWindow.passwordTextField: signUpWindow.confirmPasswordTextField.becomeFirstResponder() break case signUpWindow.confirmPasswordTextField: signUpWindow.emailTextField.becomeFirstResponder() break default: textField.resignFirstResponder() } return true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewWillDisappear(animated: Bool) { NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) } @IBAction func signup() { signUpWindow.frame=CGRectMake(signUpWindowPosition.x, signUpWindowPosition.y, 485,450) signUpWindow.backgroundColor=UIColor.clearColor() self.view.addSubview(signUpWindow) } } 

你使用DidEndOnExit连接了DidEndOnExit (我从内存中写出来,所以也许它不会调用这个完全相似的) UIControl事件,并且在这个函数中使用了textF.resignFirstResponder()或者.becomeFirstResponder()


编辑

UITextField是UIControl的子类,并以编程方式添加使用addTarget()方法的新事件。 例如:

 func a(sender: AnyObject) {} textField.addTarget(self, action: "a:", forControlEvents: .EditingDidEndOnExit) 

UIControl文档

使用标签使得它更容易。 按升序分配标签到您在屏幕上使用的所有文本字段。


 func textFieldShouldReturn(_ textField: UITextField) -> Bool { let textTag = textField.tag+1 let nextResponder = textField.superview?.viewWithTag(textTag) as UIResponder! if(nextResponder != nil) { //textField.resignFirstResponder() nextResponder?.becomeFirstResponder() } else{ // stop editing on pressing the done button on the last text field. self.view.endEditing(true) } return true }