使用“下一步”作为返回键 [英] Using "Next" as a Return Key

查看:151
本文介绍了使用“下一步”作为返回键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用返回键的下一个值来获取下一步按钮代替完成按钮,但是(显然)按下它不会自动移动到我视图中的下一个UITextField。

I use the "Next" value for the "Return Key" to get the Next button in place of the Done button, but (obviously) pressing it doesn't automatically move to the next UITextField in my view.

这样做的正确方法是什么?我见过很多答案,但是有人有快速的解决方案吗?

What's the right way to do this? I have seen many answers, but anyone have a swift solution?

推荐答案

确保你的文本字段设置了委托并实现了 textFieldShouldReturn 方法。这是当用户点击返回键时调用的方法(无论它看起来如何)。

Make sure your text fields have their delegate set and implement the textFieldShouldReturn method. This is the method that is called when the user taps the return key (no matter what it looks like).

该方法可能如下所示:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == self.field1 {
        self.field2.becomeFirstResponder()
    }

    return true
}

这里的实际逻辑可能会有所不同。有很多方法,如果你有很多文字,我肯定会建议你反对一个巨大的 if / else 链字段,但这里的要点是确定当前活动的视图,以确定哪个视图应该变为活动状态。一旦确定哪个视图应该变为活动状态,请调用该视图的 becomeFirstResponder 方法。

The actual logic in here might vary. There are numerous approaches, and I'd definitely advise against a massive if/else chain if you have lots of text fields, but the gist here is to determine what view is currently active in order to determine what view should become active. Once you've determined which view should become active, call that view's becomeFirstResponder method.

对于某些代码清洁度,您可以考虑使用 UITextField 扩展名,如下所示:

For some code cleanliness, you might consider a UITextField extension that looks something like this:

private var kAssociationKeyNextField: UInt8 = 0

extension UITextField {
    var nextField: UITextField? {
        get {
            return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
        }
        set(newField) {
            objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN)
        }
    }
}

然后将我们的 textFieldShouldReturn 方法更改为如下所示:

And then change our textFieldShouldReturn method to look like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.nextField?.becomeFirstResponder()
    return true
}

完成此操作后,只需在 viewDidLoad nextField 属性即可。 $ c>:

Once you've done this, it should simply be a matter of setting each text field's new nextField property in viewDidLoad:

self.field1.nextField = self.field2
self.field2.nextField = self.field3
self.field3.nextField = self.field4
self.field4.nextField = self.field1






虽然如果我们真的想要,我们可以在房产前加上 @IBOutlet ,这样我们就可以了在界面构建器中连接我们的nextField属性。


Although if we really wanted, we could prefix the property with @IBOutlet, and that would allow us to hook up our "nextField" property right in interface builder.

将扩展名更改为如下所示:

Change the extension to look like this:

private var kAssociationKeyNextField: UInt8 = 0

extension UITextField {
    @IBOutlet var nextField: UITextField? {
        get {
            return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
        }
        set(newField) {
            objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN)
        }
    }
}

现在在界面构建器中连接 nextField 属性:

And now hook up the nextField property in interface builder:

(在你来这里时设置你的代表。)

(Set up your delegate while you're here too.)

和当然,如果 nextField 属性返回 nil ,键盘只会隐藏。

And of course, if the nextField property returns nil, the keyboard just hides.

这篇关于使用“下一步”作为返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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