简体   繁体   English

Swift中UITextView和UITextField的单个扩展

[英]Single extension for UITextView and UITextField in Swift

I want to create a single extension for both UITextField and UITextView and add a below method to it: 我想为UITextFieldUITextView创建一个扩展 ,并向其添加以下方法:

    func addDoneButtonOnKeyboardWith(selector : Selector)
    {
        let keyBoardToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 30))
        let barButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: selector)
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        keyBoardToolBar.items = [flexibleSpace, barButtonItem]
        keyBoardToolBar.barStyle = .default
        self.inputAccessoryView = keyBoardToolBar
        self.reloadInputViews()
    }

I want the extension method to be available only for UITextField and UITextView . 我希望扩展方法仅适用于UITextFieldUITextView

Maybe this will work, since UIView is the parent class of them, downside is probably this will appear on all kinds of view, not sure if there's any other way to achieve what you need: 也许可以用,因为UIView是它们的父类,所以缺点可能是它会出现在各种视图上,不确定是否还有其他方法可以实现所需的功能:

extension UIView  {
    func addDoneButtonOnKeyboardWith(selector : Selector)
    {
        if self is UITextField || self is UITextView {
            //do something
        }
    }
}

I have an idea that could do what you want, utilising the default implementation of a protocol. 我有一个想法,可以利用协议的默认实现来做您想要的事情。 Technically you would still need two extensions but they would both be totally empty. 从技术上讲,您仍然需要两个扩展名,但它们都将完全为空。 Consider the following: 考虑以下:

Create a protocol for your method, and provide a default implementation: 为您的方法创建一个协议,并提供默认的实现:

protocol DoneButtonBearer {
    func addDoneButtonOnKeyboardWith(selector: Selector)
}

extension DoneButtonBearer {
    func addDoneButtonOnKeyboardWith(selector: Selector) {
        var view: UIView?
        defer  {
            if view != nil {
                let keyBoardToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 30))
                let barButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: selector)
                let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
                keyBoardToolBar.items = [flexibleSpace, barButtonItem]
                keyBoardToolBar.barStyle = .default
                view!.inputAccessoryView = keyBoardToolBar
                view!.reloadInputViews()
            }
        }
        if let textField = self as? UITextField {
            view = textField
            return
        }
        if let textView = self as? UITextView {
            view = textView
            return
        }
    }
}

Then just extend UITextField and UITextView with this functionality: 然后只需使用以下功能扩展UITextFieldUITextView

extension UITextField: DoneButtonBearer { }

extension UITextView: DoneButtonBearer { }

The closest class, which you can extend is UIControl, you can extend it but this method will be accessible again for UIButtons and other controls. 您可以扩展的最接近的类是UIControl,可以扩展它,但是UIButtons和其他控件将再次可以访问此方法。

So you can extend or subclass for example UIBarButtonItem with static method. 因此,您可以使用静态方法扩展或子类化例如UIBarButtonItem。

class func addDoneBarButtonOnKeyboardFor(keyInput: UIControl <UIKeyInput>, usingSelector : Selector)
{
    let keyBoardToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 30))
    let barButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: selector)
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    keyBoardToolBar.items = [flexibleSpace, barButtonItem]
    keyBoardToolBar.barStyle = .default
    keyInput.inputAccessoryView = keyBoardToolBar
    keyInput.reloadInputViews()
}

Sorry if made some mistake in Swift code. 抱歉,如果在Swift代码中出错了。 Developing on Obj C now. 现在在Obj C上进行开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Swift 中获取和设置 UITextField 和 UITextView 的光标位置 - Getting and Setting Cursor Position of UITextField and UITextView in Swift 如果在Swift中UITextField和UITextView为空,则禁用UIButton - Disable UIButton if UITextField and UITextView are empty in Swift 是否可以在UITextView和UITextField中更改单个单词的颜色 - Is it possible to change color of single word in UITextView and UITextField 触摸uitextField时如何用uitextView替换uitextField(Swift 2) - How to replace uitextField with uitextView when touching uitextField (swift 2) 快速对单个UITextField进行多次验证 - Multiple validations for a single UITextField in swift 在UITextField搜索(swift2)上突出显示特定的UITextView文本 - Highlight specific UITextView text base on UITextField search (swift2) 我可以在 swift 文本之间放置 UIButton 或 UITextField 或 UITextView - Can I put a UIButton or UITextField or UITextView between the texts in swift UITextView / UITextField:如何检查在Swift中是否按下了删除键 - UITextView/UITextField: How to check if a delete key is pressed in Swift 在Swift中使用自定义样式创建UITextField扩展 - Create UITextField Extension in Swift with custom styling UIcollectionViewCell中的UITextField的单个边框(swift 3 xcode) - single border for UITextField within a UIcollectionViewCell (swift 3 xcode)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM