0

I'm learning swift and combine framework.

I have a text field (textField) and a button (button).

Using combine framework i make sth when UITextField.textDidChangeNotification appear in NotificationCenter.

let publisher = NotificationCenter.default.publisher(for:
   UITextField.textDidChangeNotification, object: self.textField)
//more code

...and everything works fine.

I added a functionality to button - when clicked it should change text in the textField, and i can see that text has been changed, but nothing happen, like if there is no textDidChangeNotification

How can i force to send UITextField.textDidChangeNotification? When i changed text with other fucntion/button there is no textDidChangeNotification (text was not changed by a user => there was no textDidChangeNotification).

Janek Podwysocki
  • 543
  • 1
  • 6
  • 18

2 Answers2

0

It's an interesting scenario. Going through some of the class UITextField : UIControl documentation it looks like this gathers text based inputs from the user.

When your button is pressed the system is updating the text property instead of the user, so it isn't calling textDidChange. I'm not sure there would be a way to force this call, however you could just add your code from that function into your button.

Scott
  • 91
  • 6
  • Yes i can copy to the button, bu it is definately terrible solution - i have to copy the same part of code. – Janek Podwysocki Jul 03 '20 at 00:12
  • Instead of trying to use a tetdidchange function you could create your own then call the function where you need it. Keeping you from having the duplicate code. @JanekWojciechowski – Scott Jul 03 '20 at 00:15
0

Certainly i can post a notification manually for given: Notification.Name and object by:

NotificationCenter.default.post(name: UITextField.textDidChangeNotification, object: self.textField)
Janek Podwysocki
  • 543
  • 1
  • 6
  • 18