我正在尝试在单独的类中实现object CancelAfterTimer
class CancelAfter[T](getTimeout: T => FiniteDuration) extends GraphStage[FlowShape[T, T]] {
val in = Inlet[T]("CancelAfter.in")
val out = Outlet[T]("CancelAfter.in")
override val shape: FlowShape[T, T] = FlowShape(in, out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new TimerGraphStageLogic(shape) with InHandler with OutHandler {
override def onPush(): Unit = {
val elem = grab(in)
if (!isTimerActive(CancelAfterTimer))
scheduleOnce(CancelAfterTimer, getTimeout(elem))
push(out, elem)
}
override def onTimer(timerKey: Any): Unit =
completeStage() //this will cancel the upstream and close the downstrean
override def onPull(): Unit = pull(in)
setHandlers(in, out, this)
}
}
,但它没有工作:
UITextFieldDelegate
但这有效:
class ViewController: UIViewController {
@IBOutlet var TextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let restrictor = TextFieldRestrictController()
TextField.delegate = restrictor
}
}
class TextFieldRestrictController : NSObject, UITextFieldDelegate {
public func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
let inverseSet = NSCharacterSet(
charactersIn:"0123456789.").inverted
let components = string.components(separatedBy: inverseSet)
let filtered = components.joined(separator:"")
return string == filtered
}
}
有什么问题吗?
答案 0 :(得分:2)
UITextField
的{{3}}是弱属性,因此在viewDidLoad
方法结束时,
restrictor
将被释放,因为周围没有其他强引用指向它所指向的对象。
您可以尝试在ValidationTextField.delegate
中打印viewDidAppear(animated:)
,然后看到它返回nil
。
要解决此问题,您可以将restrictor
定义为实例变量,只要视图控制器处于活动状态,它就会保持活动状态:
class ViewController: UIViewController {
@IBOutlet weak var ValidationTextField: UITextField!
let restrictor = TextFieldRestrictController()
override func viewDidLoad() {
super.viewDidLoad()
ValidationTextField.delegate = restrictor
}
}