Implementing UITextFieldDelegate with Swift

前端 未结 12 1219
后悔当初
后悔当初 2020-12-08 08:11

I have my ViewController class which implements UITextFieldDelegate. I have no auto complete for the funcs such as textFieldShouldBeginEditing. Is this a bug in XCode 6?

相关标签:
12条回答
  • 2020-12-08 08:53

    Xcode 6 (Beta 1) is not currently supporting autocomplete for non-implemented protocol methods/properties (for Swift).

    Your best bet is to <CMD> - click on the protocol that isn't yet fully implemented to see what you're missing.

    0 讨论(0)
  • 2020-12-08 08:54

    Swift 4:

    @IBOutlet weak var yourNameTextField: UITextField! {
            didSet {
                yourNameTextField.delegate = self
            }
    }
    
    
    extension YourNameViewController: UITextFieldDelegate {
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            switch textField {
            case yourNameTextField:
                yourNameTextField.resignFirstResponder()
            default:
                break
            }
            return true
        }
    
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            return true
        }
    }
    
    0 讨论(0)
  • 2020-12-08 08:57

    A bit more swifty is ...

    @IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
    
    0 讨论(0)
  • 2020-12-08 09:00

    I spent a long night trying to fix this and the problem was that my coworker implemented all the logic in textFieldShouldBeginEditing instead of textFieldDidBeginEditing and sometimes textFieldShouldBeginEditing was not called when I used the first responder between TextFields ...

    0 讨论(0)
  • 2020-12-08 09:01
    Swift 3.0.1
    
     // UITextField Delegates
        func textFieldDidBeginEditing(_ textField: UITextField) {
        }
        func textFieldDidEndEditing(_ textField: UITextField) {
        }
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            return true;
        }
        func textFieldShouldClear(_ textField: UITextField) -> Bool {
            return true;
        }
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            return true;
        }
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            return true;
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder();
            return true;
        }
    
    0 讨论(0)
  • 2020-12-08 09:02

    // MARK:- ---> Textfield Delegates

    func textFieldDidBeginEditing(textField: UITextField) {
    
        print("TextField did begin editing method called")
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
    
        print("TextField did end editing method called\(textField.text)")
    }
    
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    
        print("TextField should begin editing method called")
        return true;
    }
    
    func textFieldShouldClear(textField: UITextField) -> Bool {
    
        print("TextField should clear method called")
        return true;
    }
    
    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        print("TextField should end editing method called")
        return true;
    }
    
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        print("While entering the characters this method gets called")
        return true;
    }
    
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
    
        print("TextField should return method called")
        textField.resignFirstResponder();
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题