如何在Swift中的单独类中设置UITextField的委托?

时间:2017-05-17 08:57:44

标签: swift delegates uitextfield uitextfielddelegate

我正在尝试在单独的类中实现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
    }
}

有什么问题吗?

1 个答案:

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