在Swift中按回车键切换文本字段 [英] Switching between Text fields on pressing return key in Swift

查看:100
本文介绍了在Swift中按回车键切换文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个iOS应用程序,我希望在我的iPhone中按下返回键时,它会将我引导到下一个文本字段。

I'm designing an iOS app and I want that when the return key is pressed in my iPhone it directs me to the next following text field.

我有发现了几个类似的问题,周围有很好的答案,但它们恰好都在Objective-C中,我正在寻找Swift代码,现在这就是我现在所拥有的:

I have found a couple of similar questions, with excellent answers around but they all just happen to be in Objective-C and I'm looking for Swift code, now this is what I have up until now:

func textFieldShouldReturn(emaillabel: UITextField) -> Bool{
    return true
}

它被放置在已连接的文件中包含文本字段的UIView的控制器,但我不确定这是不是正确的地方。

It's placed in the file that's connected and controller to the UIView that contains the text fields, but I'm not sure if thats the right place.

我对swift来说基本上是新手,所以解释每一小步或者我会以某种方式搞砸它。我也在使用最新版本的Xcode,如果这有任何区别。

I'm basically new to swift, so explain every little step or i'll manage to mess it up somehow. Also I'm using the latest version of Xcode if that makes any difference.

好吧,所以我试了这个并得到了这个错误:

//无法找到接受所提供参数的'!='的重载

okay, so I tried this out and got this error:
//could not find an overload for '!=' that accepts the supplied arguments

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let nextTag: NSInteger = textField.tag + 1;
    // Try to find next responder
    let nextResponder: UIResponder = textField.superview!.viewWithTag(nextTag)!
    if (nextResponder != nil) {//could not find an overload for '!=' that accepts the supplied arguments

        // Found next responder, so set it.
        nextResponder.becomeFirstResponder()
    } else {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
    }
    return false; // We do not want UITextField to insert line-breaks.
}

先谢谢好友!!

推荐答案

Swift 3.0 (查看以前版本的编辑历史记录):

Swift 3.0 (check edit history for previous versions):

class ViewController: UIViewController,UITextFieldDelegate 
{
   //Link each UITextField
   @IBOutlet weak var textField: UITextField!

   override func viewDidLoad() 
   {
      super.viewDidLoad()
      // Do this for each UITextField
      textField.delegate = self
      textField.tag = 0 //Increment accordingly
   }

   func textFieldShouldReturn(_ textField: UITextField) -> Bool 
   {
      // Try to find next responder
      if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
         nextField.becomeFirstResponder()
      } else {
         // Not found, so remove keyboard.
         textField.resignFirstResponder()
      }
      // Do not add a line break
      return false
   }
}

确保设置了 UITextField 委托,并且标签正确递增。这也可以通过Interface Builder完成。

Make sure your UITextField delegates are set and the tags are incremented properly. This can also be done through the Interface Builder.

这是我找到的Obj-C帖子的链接:如何浏览文本字段(下一个/完成按钮)

Here's a link to an Obj-C post I found: How to navigate through textfields (Next / Done Buttons)

这篇关于在Swift中按回车键切换文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆