Ios 使用警报控制器中的“下一步”和“上一步”按钮将工具栏添加到十进制键盘

Ios 使用警报控制器中的“下一步”和“上一步”按钮将工具栏添加到十进制键盘,ios,xcode,uialertcontroller,uitoolbar,custom-keyboard,Ios,Xcode,Uialertcontroller,Uitoolbar,Custom Keyboard,我有下面的功能,添加了“下一步”和“上一步”按钮,可以在文本字段之间切换,以获得更好的用户体验。但是,我无法通过警报控制器中的文本字段实现这一点。是否有解决方案,或者我必须为我的应用程序考虑另一种设计 extension UIViewController { func addInputAccessoryForTextFields(textFields: [UITextField], dismissable: Bool = true, previousNextable: Bool = false)

我有下面的功能,添加了“下一步”和“上一步”按钮,可以在文本字段之间切换,以获得更好的用户体验。但是,我无法通过警报控制器中的文本字段实现这一点。是否有解决方案,或者我必须为我的应用程序考虑另一种设计

extension UIViewController {
func addInputAccessoryForTextFields(textFields: [UITextField], dismissable: Bool = true, previousNextable: Bool = false) {
    for (index, textField) in textFields.enumerated() {
        let toolbar: UIToolbar = UIToolbar()
        toolbar.sizeToFit()
        var items = [UIBarButtonItem]()
        if previousNextable {
            let previousButton = UIBarButtonItem(image: UIImage(named: "previous"), style: .plain, target: nil, action: nil)
            previousButton.width = 30
            if textField == textFields.first {
                previousButton.isEnabled = false
            } else {
                previousButton.target = textFields[index - 1]
                previousButton.action = #selector(UITextField.becomeFirstResponder)
            }
            let nextButton = UIBarButtonItem(image: UIImage(named: "next"), style: .plain, target: nil, action: nil)
            nextButton.width = 30
            if textField == textFields.last {
                nextButton.isEnabled = false
            } else {
                nextButton.target = textFields[index + 1]
                nextButton.action = #selector(UITextField.becomeFirstResponder)
            }
            items.append(contentsOf: [previousButton, nextButton])
        }
        let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: view, action: #selector(UIView.endEditing))
        items.append(contentsOf: [spacer, doneButton])
        toolbar.setItems(items, animated: false)
        textField.inputAccessoryView = toolbar
    }
}
}
这是我的警报控制器,我在这里声明警报并添加它们:

alertDisplay.addTextField { (textField) in
        textField.placeholder = "Enter Team 1 Score"
        textField.keyboardType = UIKeyboardType.decimalPad

    }
    alertDisplay.addTextField { (textField) in
        textField.placeholder = "Enter Team 2 Score"
        textField.keyboardType = UIKeyboardType.decimalPad
    }
要调用该函数,我必须在数组中添加所需的文本字段(这将允许下一个和上一个遍历文本字段)。此函数在viewDidLoad()方法中调用


因此,问题是如何使警报中的两个文本字段具有用于键盘的自定义工具栏

我建议改为使用自定义视图作为警报,因为这种自定义级别更容易实现。@4kman我从未想过这一点。意识到这一点太愚蠢了!非常感谢。