【发布时间】:2015-10-24 08:32:57
【问题描述】:
我正在设计一个 iOS 应用程序,我希望在我的 iPhone 中按下返回键时,它会将我引导到下一个文本字段。
我发现了几个类似的问题,周围都有很好的答案,但它们都恰好在 Objective-C 中,我正在寻找 Swift 代码,现在这是我到目前为止所拥有的:
func textFieldShouldReturn(emaillabel: UITextField) -> Bool{
return true
}
它被放置在连接到包含文本字段的 UIView 的文件和控制器中,但我不确定那是否是正确的位置。
好的,所以我试了一下,得到了这个错误://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
标签: ios swift uitextfielddelegate