在 UITextFieldDelegate 函数中使用自定义 UITextField
Using custom UITextField in UITextFieldDelegate functions
我正在尝试在 Swift 中创建自定义 UI 文本字段。这是我的代码
public class AmountTextField : UITextField{
let currencyFormattor = NSNumberFormatter()
let amountTextFieldDelegate = AmountTextFieldDelegate()
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initTextField()
}
func initTextField(){
self.delegate = amountTextFieldDelegate
currencyFormattor.numberStyle = .CurrencyStyle
currencyFormattor.minimumFractionDigits = 2
currencyFormattor.maximumFractionDigits = 2
}
func setAmount (amount : Double){
let textFieldStringValue = currencyFormattor.stringFromNumber(amount)
self.text = textFieldStringValue
}
}
我的 AmountTextFieldDelegate 看起来像这样
class AmountTextFieldDelegate : NSObject, UITextFieldDelegate{
func textField(textField: AmountTextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let amount = getAmount() // calculates the amount
textField.setAmount(amount)
return false
}
}
我在 shouldChangeCharactersInRange 中将 UITextField 更改为 AmountTextField,这样我就可以在 shouldChangeCharactersInRange 中调用 setAmount 函数。但是当我这样做时,我得到了错误 -
方法'textField(:shouldChangeCharactersInRange:replacementString:)'提供的
Objective-C方法'textField:shouldChangeCharactersInRange:replacementString:'与协议'UITextFieldDelegate'[=12=中的可选要求方法'textField(
:shouldChangeCharactersInRange:replacementString:)'冲突]
有没有办法在 shouldChangeCharactersInRange 中调用 setAmount?
考虑一下:
class AmountTextFieldDelegate : NSObject, UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let s = textField as? AmountTextField {
let amount = s.getAmount() // calculates the amount
s.setAmount(amount)
}
return true
}
}
我正在尝试在 Swift 中创建自定义 UI 文本字段。这是我的代码
public class AmountTextField : UITextField{
let currencyFormattor = NSNumberFormatter()
let amountTextFieldDelegate = AmountTextFieldDelegate()
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initTextField()
}
func initTextField(){
self.delegate = amountTextFieldDelegate
currencyFormattor.numberStyle = .CurrencyStyle
currencyFormattor.minimumFractionDigits = 2
currencyFormattor.maximumFractionDigits = 2
}
func setAmount (amount : Double){
let textFieldStringValue = currencyFormattor.stringFromNumber(amount)
self.text = textFieldStringValue
}
}
我的 AmountTextFieldDelegate 看起来像这样
class AmountTextFieldDelegate : NSObject, UITextFieldDelegate{
func textField(textField: AmountTextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let amount = getAmount() // calculates the amount
textField.setAmount(amount)
return false
}
}
我在 shouldChangeCharactersInRange 中将 UITextField 更改为 AmountTextField,这样我就可以在 shouldChangeCharactersInRange 中调用 setAmount 函数。但是当我这样做时,我得到了错误 -
方法'textField(:shouldChangeCharactersInRange:replacementString:)'提供的
Objective-C方法'textField:shouldChangeCharactersInRange:replacementString:'与协议'UITextFieldDelegate'[=12=中的可选要求方法'textField(
有没有办法在 shouldChangeCharactersInRange 中调用 setAmount?
考虑一下:
class AmountTextFieldDelegate : NSObject, UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let s = textField as? AmountTextField {
let amount = s.getAmount() // calculates the amount
s.setAmount(amount)
}
return true
}
}