Swift 2 addObserver for specific UITextfield with object parameter

783 Views Asked by At

I'd like to call a function if a specific UITextField was tapped: As far as I understand the following code works for notifications, which comes from the window:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

But I'd like to have a specific notification from a specific UITextField. I tried the following code without success:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UITextFieldTextDidBeginEditingNotification, object: textField)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UITextFieldTextDidEndEditingNotification, object: textField)

I searched a lot and found that same question here (Swift 2 addObserver for specific textField with the object parameter), but the answers didn't work for me.

I'm trying that for 3 hours now.... Any help is appreciated, thank you very much!

1

There are 1 best solutions below

2
On BEST ANSWER

You may look into UITextFieldDelegate protocol - there you could find solution to your problem. Just set your class as delegate, and implement one of methods of protocol. You can find them in documentation(for example textFieldShouldBeginEditing). If you have any problems, feel free to ask questions.