Material TextField上一个下一个按钮

时间:2017-03-21 08:30:03

标签: ios swift

当我选择了textField时,我试图在键盘顶部显示上一个/下一个箭头按钮。

我试过以下

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
            }
        }
    }
}

0 个答案:

没有答案