I would like to be able to detect if some text is changed in a UITextField
so that I can then enable a UIButton
to save the changes.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- how do you prevent page scroll in textarea on mobi
- iOS (objective-c) compression_decode_buffer() retu
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
For that, first you need to have your textfield have it delegate reference assigned. And the delgate, should preferably be, the vew controller which is the files owner of the view. Which goes like
And in your viewController interface, tell you will be implementing UITextFieldDelegate by
And in your view controller implementation override
So the code will look like
You can also subscribe to notification which is sort of messy IMHO You can find how to do it here.
Swift 3.0
Process 1
Create IBOutlet of UITextfiled and Add Target to text field.
Process 2
You can add a class member like this
NSString *changeTemp
,thenTake advantage of the UITextFieldTextDidChange notification or set a delegate on the text field and watch for textField:shouldChangeCharactersInRange:replacementString.
If you want to watch for changes with a notification, you'll need something like this in your code to register for the notification:
Here theTextField is the instance of UITextField that you want to watch. The class of which self is an instance in the code above must then implement textFieldDidChange, like so:
If the text field is going to outlive the observer, then you must deregister for notifications in the observer's dealloc method. Actually it's a good idea to do this even if the text field does not outlive the observer.
This can be accomplished in Interface Builder on the
Editing Changed
event ofUITextField
. Drag from it to your code and create anIBAction
.For example:
This event is the same as described in other answers here in that the
.text
property contains the updated text input when it gets triggered. This can help clean up code clutter by not having to programmatically add the event to everyUITextField
in the view.Instead of observing notifications or implementing
textField:shouldChangeCharacterInRange:replacementString:
, it's easier to just add an event target:Note that the event is
UIControlEventEditingChanged
and notUIControlEventValueChanged
!The advantages over the other two suggested solutions are:
NSNotificationCenter
.textField.text
contains the text the user actually entered. ThetextField:shouldChangeCharacterInRange:replacementString:
delegate method is called before the changes have been applied, sotextField.text
does not yet give you the text the user just entered – you'd have to apply the change yourself first.