ios - 如何在 'doneButtonAction' 方法中获取 textField id,同时在 swift 中使用多个 numPad 键盘文本字段?

标签 ios swift keyboard textfield

我正在使用两个 numPad 键盘类型的 textField。我在 numPad 键盘中为第一个 textField 添加了 Next 按钮,为第二个 textField 添加了 Done 按钮。使用以下代码。

import UIKit

class SecurityPinSettingsVC: UIViewController, UITextFieldDelegate
{
@IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
 override func viewDidLoad()
    {
        super.viewDidLoad()    
        self.textField1.delegate = self
        self.textField2.delegate = self
        self.textField1.textAlignment = NSTextAlignment.center
        self.textField2.textAlignment = NSTextAlignment.center
        addNextButtonOnKeyboard()
        addDoneButtonOnKeyboard()        
    }
func addNextButtonOnKeyboard()
    {
        let nextToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0,y: 0, width: 320, height: 50))
        nextToolbar.barStyle       = UIBarStyle.default
        let flexSpace              = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let next: UIBarButtonItem  = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.doneButtonAction))


        var items = [UIBarButtonItem]()
        items.append(flexSpace)
        items.append(next)

        nextToolbar.items = items
        nextToolbar.sizeToFit()

        self.textField1.inputAccessoryView = nextToolbar
    }



    func addDoneButtonOnKeyboard()
    {
        let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
        doneToolbar.barStyle       = UIBarStyle.default
        let flexSpace              = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let ddone: UIBarButtonItem  = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.doneButtonAction))

        var items = [UIBarButtonItem]()
        items.append(flexSpace)
        items.append(ddone)

        doneToolbar.items = items
        doneToolbar.sizeToFit()

              self.textField2.inputAccessoryView = doneToolbar
    }
 func doneButtonAction()
{
        //self.textField2.becomeFirstResponder()
        //self.textField2.resignFirstResponder()
}

}

两个按钮都添加成功。问题是,我无法为 Next 按钮和 Done 按钮创建单独的操作方法。它只接受两个按钮的 doneButtonAction 方法,这是我在以前的 viewControllers 中创建和使用的。那么,如何在 doneButtonAction 方法中获取 textField id,以便知道单击了哪个按钮。因此,通过单击 Next 按钮,我想让 textField2 成为第一响应者,通过单击 Done 按钮,我希望 textField2 退出第一响应者。

与普通的 textField 一样,textFieldShouldReturn 方法可以轻松完成这项工作,其中 textField 对象将作为参数传递给该函数。

在这里,我们如何将 textField 对象传递给 doneButtonAction 方法。

我正在使用 Xcode 8.2、Swift 3.0

提前致谢。

最佳答案

您可以获得成为响应者的文本字段

if textField1.isFirstResponder {
    // textfield 1 is becomeResponder 
}

关于ios - 如何在 'doneButtonAction' 方法中获取 textField id,同时在 swift 中使用多个 numPad 键盘文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619850/

相关文章:

ios - Xcode,cocoapods + nib 文件,s.resources = ['*.{xib}'] 不工作

ios - Firebase 增量值工作了一段时间然后中断

iOS YTPlayer 暂停时黑屏

ios - 键盘滞后三秒,首次仅使用 iPad - Obj-C

ios - Objective c 键盘不情愿地打开

ios - 将 UITextView 的 inputview 属性从自定义 View 更改为 nil 会导致崩溃

ios - 根据传入的值显示大量文本

ios - AppConnect:错误:AppConnect无法启动,因为[UIApplication sharedApplication]不是AppConnectUIApplication的实例

ios - 检测 iOS 8 quicktype 键盘在没有通知的情况下打开或关闭

ios - 条形按钮项目在 Swift 中启用问题