我正在设计一款iOS应用程序,我希望当我的iPhone按下Return键时,它会将我 bootstrap 到下面的文本字段.

我已经找到了几个类似的问题,答案都很不错,但它们都恰好在Objective-C中,我正在寻找SWIFT代码,现在我有的是到目前为止的情况:

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

它放在连接并控制到包含文本字段的UIView的文件中,但我不确定这是不是正确的位置.

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.
}

推荐答案

确保您的UITextField个代理已设置,并且标签已正确递增.这也可以通过接口生成器来完成.

这是我找到的一个Obj-C帖子的链接:How to navigate through textfields (Next / Done Buttons)

class ViewController: UIViewController,UITextFieldDelegate {
   // Link each UITextField (Not necessary if delegate and tag are set in Interface Builder)
   @IBOutlet weak var someTextField: UITextField!

   override func viewDidLoad() {
      super.viewDidLoad()
      // Do the next two lines for each UITextField here or in the Interface Builder
      someTextField.delegate = self
      someTextField.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
   }
}

Ios相关问答推荐

Swift:从字符串目录获取带有参数的本地化字符串

在SwiftData中从@ Query创建可排序、有序和分组的数据

围绕对象旋转和移动相机不起作用Swift SceneKit

swiftui 图像放大时无法正确拖动

在自定义 ButtonStyle 中修改形状 colored颜色

创建 Flutter 项目时,如何从我的系统中删除特定的开发者身份?

CarPlay:现在播放alert 可能吗?

Task.cancel() 的 TaskPriority 的区别

如何在 Mac OS Ventura 中使用 Xcode 13

如何在 SwiftUI 中将选项卡放在滚动视图中?

SwiftUI 中的偏移量和按钮有问题吗?

类型任何视图不能符合具有泛型的协议上的视图

iOS - 确保在主线程上执行

interfaceOrientation在 iOS 8 中已弃用,如何更改此方法 Objective C

Swift:调用中缺少参数标签xxx

命令 /Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang 失败,退出代码为 1

文本未包含在 swift UI 中

有什么方法可以加粗 NSString 的一部分?

如果我的分发证书过期会怎样?

如何为 UILabel 的背景 colored颜色 设置动画?