[英]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: 我想为
UITextField
和UITextView
创建一个扩展 ,并向其添加以下方法:
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
. 我希望扩展方法仅适用于
UITextField
和UITextView
。
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: 然后只需使用以下功能扩展
UITextField
和UITextView
:
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.