How do I check when a UITextField changes?

后端 未结 19 2978
情话喂你
情话喂你 2020-11-22 15:44

I am trying to check when a text field changes, equivalent too the function used for textView - textViewDidChange so far I have done this:

  fu         


        
相关标签:
19条回答
  • 2020-11-22 16:33

    The way I've handled it so far: in UITextFieldDelegate

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
    {
        // text hasn't changed yet, you have to compute the text AFTER the edit yourself
        let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
    
        // do whatever you need with this updated string (your code)
    
    
        // always return true so that changes propagate
        return true
    }
    

    Swift4 version

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
        return true
    }
    
    0 讨论(0)
  • 2020-11-22 16:33

    Swift 4

    textField.addTarget(self, action: #selector(textIsChanging), for: UIControlEvents.editingChanged)
    
    @objc func textIsChanging(_ textField:UITextField) {
    
     print ("TextField is changing")
    
    }
    

    If you want to make a change once the user has typed in completely (It will be called once user dismiss keyboard or press enter).

    textField.addTarget(self, action: #selector(textDidChange), for: UIControlEvents.editingDidEnd)
    
     @objc func textDidChange(_ textField:UITextField) {
    
           print ("TextField did changed") 
     }
    
    0 讨论(0)
  • 2020-11-22 16:34

    Maybe use RxSwift ?

    need

    pod 'RxSwift',    '~> 3.0'
    pod 'RxCocoa',    '~> 3.0'
    

    add imports obviously

    import RxSwift
    import RxCocoa
    

    So u have a textfield : UITextField

    let observable: Observable<String?> = textField.rx.text.asObservable()
    observable.subscribe(
                onNext: {(string: String?) in
                    print(string!)
            })
    

    U have other 3 methods..

    1. onError
    2. onCompleted
    3. onDisposed
    4. onNext
    0 讨论(0)
  • 2020-11-22 16:37

    Swift 3

     textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(sender:)), for: UIControlEvents.editingChanged)
    
    0 讨论(0)
  • 2020-11-22 16:38

    Swift 4.2

    write this in viewDidLoad

    // to detect if TextField changed
    TextField.addTarget(self, action: #selector(textFieldDidChange(_:)),
                                       for: UIControl.Event.editingChanged)
    

    write this outside viewDidLoad

    @objc func textFieldDidChange(_ textField: UITextField) {
        // do something
    }
    

    You could change the event by UIControl.Event.editingDidBegin or what ever you want to detect.

    0 讨论(0)
  • 2020-11-22 16:39

    SWIFT

    Swift 4.2

    textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
    

    and

    @objc func textFieldDidChange(_ textField: UITextField) {
    
    }
    

    SWIFT 3 & swift 4.1

    textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
    

    and

    func textFieldDidChange(_ textField: UITextField) {
    
    }
    

    SWIFT 2.2

    textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
    

    and

    func textFieldDidChange(textField: UITextField) {
        //your code
    }
    

    OBJECTIVE-C

    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    

    and textFieldDidChange method is

    -(void)textFieldDidChange :(UITextField *) textField{
        //your code
    }
    
    0 讨论(0)
提交回复
热议问题