Ios 材料文本字段上一个下一个按钮

Ios 材料文本字段上一个下一个按钮,ios,swift,Ios,Swift,当选择文本字段时,我试图在键盘顶部获得上一个/下一个箭头按钮 我试过以下方法 textField0.tag = 0 // textField0 : TextField textField0.addPreviousNextRightOnKeyboardWithTarget(self, rightButtonTitle: "Submit", previousAction: #selector(previousTextField(sender:)), nextAction: #selector(nex

当选择文本字段时,我试图在键盘顶部获得上一个/下一个箭头按钮

我试过以下方法

textField0.tag = 0 // textField0 : TextField
textField0.addPreviousNextRightOnKeyboardWithTarget(self, rightButtonTitle: "Submit", previousAction: #selector(previousTextField(sender:)), nextAction: #selector(nextTextField(sender:)), rightButtonAction: #selector(saveNewPlan), titleText: nil)
textField1.tag = 1
textField1.addPreviousNextRightOnKeyboardWithTarget(self, rightButtonTitle: "Submit", previousAction: #selector(previousTextField(sender:)), nextAction: #selector(nextTextField(sender:)), rightButtonAction: #selector(saveNewPlan), titleText: nil)
定义为上一个/下一个操作选择器

func previousTextField(sender: AnyObject){
    // I actually get the sender as IQBarButtonItem (subclass of UIBarButtonItem) instead of UITextField here
    if let previousField = textField.superview?.viewWithTag(textField.tag - 1) as? UITextField {
        _ = previousField.becomeFirstResponder()
    }
}
func nextTextField(sender: AnyObject){
    if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
        _ = nextField.becomeFirstResponder()
    }
}
每当我点击按钮,它就会抛出这个错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IQKeyboardManagerSwift.IQBarButtonItem superview]: unrecognized selector sent to instance 0x7fc3775658e0'
下面是该方法的作用

public func addPreviousNextRightOnKeyboardWithTarget( _ target : AnyObject?, rightButtonTitle : String, previousAction : Selector, nextAction : Selector, rightButtonAction : Selector, titleText : String?) {

    //If can't set InputAccessoryView. Then return
    if self.responds(to: #selector(setter: UITextField.inputAccessoryView)) {
        //  Creating a toolBar for phoneNumber keyboard
        let toolbar = IQToolbar()
        toolbar.doneTitle = rightButtonTitle

        var items : [UIBarButtonItem] = []

        let prev : IQBarButtonItem
        let next : IQBarButtonItem

        // Get the top level "bundle" which may actually be the framework
        var bundle = Bundle(for: IQKeyboardManager.self)

        if let resourcePath = bundle.path(forResource: "IQKeyboardManager", ofType: "bundle") {
            if let resourcesBundle = Bundle(path: resourcePath) {
                bundle = resourcesBundle
            }
        }

        var imageLeftArrow : UIImage!
        var imageRightArrow : UIImage!

        if #available(iOS 10.0, *) {
            imageLeftArrow = UIImage(named: "IQButtonBarArrowUp", in: bundle, compatibleWith: nil)
            imageRightArrow = UIImage(named: "IQButtonBarArrowDown", in: bundle, compatibleWith: nil)
        } else {
            imageLeftArrow = UIImage(named: "IQButtonBarArrowLeft", in: bundle, compatibleWith: nil)
            imageRightArrow = UIImage(named: "IQButtonBarArrowRight", in: bundle, compatibleWith: nil)
        }

        //Support for RTL languages like Arabic, Persia etc... (Bug ID: #448)
        if #available(iOS 9.0, *) {
            imageLeftArrow = imageLeftArrow?.imageFlippedForRightToLeftLayoutDirection()
            imageRightArrow = imageRightArrow?.imageFlippedForRightToLeftLayoutDirection()
        }

        prev = IQBarButtonItem(image: imageLeftArrow, style: UIBarButtonItemStyle.plain, target: target, action: previousAction)
        prev.accessibilityLabel = "Toolbar Previous Button"

        next = IQBarButtonItem(image: imageRightArrow, style: UIBarButtonItemStyle.plain, target: target, action: nextAction)
        next.accessibilityLabel = "Toolbar Next Button"

        //Previous button
        items.append(prev)

        //Fixed space
        let fixed = IQBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        if #available(iOS 10.0, *) {
            fixed.width = 6
        } else {
            fixed.width = 20
        }
        items.append(fixed)

        //Next button
        items.append(next)

        //Flexible space
        items.append(UIView.flexibleBarButtonItem())

        //Title button
        let title = IQTitleBarButtonItem(title: shouldHidePlaceholderText == true ? nil : titleText)
        items.append(title)

        //Flexible space
        items.append(UIView.flexibleBarButtonItem())

        //Right button
        let doneButton = IQBarButtonItem(title: rightButtonTitle, style: UIBarButtonItemStyle.done, target: target, action: rightButtonAction)
        items.append(doneButton)

        //  Adding button to toolBar.
        toolbar.items = items
        toolbar.toolbarTitleInvocation = self.titleInvocation

        //  Setting toolbar to keyboard.
        if let textField = self as? UITextField {
            textField.inputAccessoryView = toolbar

            switch textField.keyboardAppearance {
            case UIKeyboardAppearance.dark:
                toolbar.barStyle = UIBarStyle.black
            default:
                toolbar.barStyle = UIBarStyle.default
            }
        } else if let textView = self as? UITextView {
            textView.inputAccessoryView = toolbar

            switch textView.keyboardAppearance {
            case UIKeyboardAppearance.dark:
                toolbar.barStyle = UIBarStyle.black
            default:
                toolbar.barStyle = UIBarStyle.default
            }
        }
    }
}

您正在使用第三方键盘管理器
IQKeyboardManagerSwift
。错误来自于此。您需要共享您在此viewcontroller中如何使用IQKeyboardManagerSwift的代码。@更新了。previousAction和nextAction方法定义在哪里?我已经在前面发布了它们,
func previousTextField(textField:UITextField)
&
func nextTextField(textField:UITextField)
我只是想看看我收到的是哪种类型的发件人,结果是
iqbarbuttoneim
,它是
uibarbuttoneim