Unexpected behaviour text textFieldShouldBeginEditing / textFieldDidBeginEditing
I have a UItextField
called textField
on a static cell tableView. That field contains a currency amount - like "$10,000.00".
When editing that amount, the currency and thousand grouping symbols are a bit in the way. Therefore I would like to remove those when the field becomes the first responder.
I do this in textFieldShouldBeginEditing
.
The first time I do this, everything works. The textField
's contents are reformatted without currency and thousand group separator.
On textFieldDidEndEditing
I re-format the value into a proper currency string again. This also works.
The problem happens when I re-enter the field for a 2nd time. While debugging I can see the textField.text
has changed into the string without currency symbol and grouping symbol, but the display does not show that. Although it did work the first time! The 2nd time it looks like there is a mismatch between what is on the screen and what value the debugger sees.
I've tried things like:
- tableView.beginUpdates(); tableView.endUpdates()
- textView.setNeedsDisplay
... but this does not work.
So I copied the code that removes the currency formatting in textFieldShouldBeginEditing
to a new delegate method textFieldDidBeginEditing
.
Then everything worked fine. I could tap other controls and back to the textField a number of times and each time the control would lose its formatting when entered and would be restored to a formatted currency string after losing focus.
So I decided to delete the method textFieldShouldBeginEditing
. But then things broke down again! It looks like I have to implement both textFieldShouldBeginEditing
as well as textFieldDidBeginEditing
to be able to prepare a textField
's contents for user-editing?
Is this a bug?
extension Double
public func doubleToString(numberStyle: NumberFormatter.Style, decimals: Int, withThousandSeparator: Bool) -> String
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = numberStyle
numberFormatter.maximumFractionDigits = decimals
if !withThousandSeparator
numberFormatter.groupingSeparator = ""
return numberFormatter.string(from: NSNumber(value: self)) ?? ""
func textFieldDidBeginEditing(_ textField: UITextField)
if textField === self.textField
textField.text = amount.doubleToString(numberStyle: .decimal, decimals: 2, withThousandSeparator: false)
ios swift uitableview swift3 uitextfield
add a comment |
I have a UItextField
called textField
on a static cell tableView. That field contains a currency amount - like "$10,000.00".
When editing that amount, the currency and thousand grouping symbols are a bit in the way. Therefore I would like to remove those when the field becomes the first responder.
I do this in textFieldShouldBeginEditing
.
The first time I do this, everything works. The textField
's contents are reformatted without currency and thousand group separator.
On textFieldDidEndEditing
I re-format the value into a proper currency string again. This also works.
The problem happens when I re-enter the field for a 2nd time. While debugging I can see the textField.text
has changed into the string without currency symbol and grouping symbol, but the display does not show that. Although it did work the first time! The 2nd time it looks like there is a mismatch between what is on the screen and what value the debugger sees.
I've tried things like:
- tableView.beginUpdates(); tableView.endUpdates()
- textView.setNeedsDisplay
... but this does not work.
So I copied the code that removes the currency formatting in textFieldShouldBeginEditing
to a new delegate method textFieldDidBeginEditing
.
Then everything worked fine. I could tap other controls and back to the textField a number of times and each time the control would lose its formatting when entered and would be restored to a formatted currency string after losing focus.
So I decided to delete the method textFieldShouldBeginEditing
. But then things broke down again! It looks like I have to implement both textFieldShouldBeginEditing
as well as textFieldDidBeginEditing
to be able to prepare a textField
's contents for user-editing?
Is this a bug?
extension Double
public func doubleToString(numberStyle: NumberFormatter.Style, decimals: Int, withThousandSeparator: Bool) -> String
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = numberStyle
numberFormatter.maximumFractionDigits = decimals
if !withThousandSeparator
numberFormatter.groupingSeparator = ""
return numberFormatter.string(from: NSNumber(value: self)) ?? ""
func textFieldDidBeginEditing(_ textField: UITextField)
if textField === self.textField
textField.text = amount.doubleToString(numberStyle: .decimal, decimals: 2, withThousandSeparator: false)
ios swift uitableview swift3 uitextfield
What do you do in textFieldShouldBeginEditing ? Just return true ?
– claude31
Nov 15 '18 at 19:57
Would you mind adding the code to reproduce the issue?
– Kamran
Nov 15 '18 at 22:37
add a comment |
I have a UItextField
called textField
on a static cell tableView. That field contains a currency amount - like "$10,000.00".
When editing that amount, the currency and thousand grouping symbols are a bit in the way. Therefore I would like to remove those when the field becomes the first responder.
I do this in textFieldShouldBeginEditing
.
The first time I do this, everything works. The textField
's contents are reformatted without currency and thousand group separator.
On textFieldDidEndEditing
I re-format the value into a proper currency string again. This also works.
The problem happens when I re-enter the field for a 2nd time. While debugging I can see the textField.text
has changed into the string without currency symbol and grouping symbol, but the display does not show that. Although it did work the first time! The 2nd time it looks like there is a mismatch between what is on the screen and what value the debugger sees.
I've tried things like:
- tableView.beginUpdates(); tableView.endUpdates()
- textView.setNeedsDisplay
... but this does not work.
So I copied the code that removes the currency formatting in textFieldShouldBeginEditing
to a new delegate method textFieldDidBeginEditing
.
Then everything worked fine. I could tap other controls and back to the textField a number of times and each time the control would lose its formatting when entered and would be restored to a formatted currency string after losing focus.
So I decided to delete the method textFieldShouldBeginEditing
. But then things broke down again! It looks like I have to implement both textFieldShouldBeginEditing
as well as textFieldDidBeginEditing
to be able to prepare a textField
's contents for user-editing?
Is this a bug?
extension Double
public func doubleToString(numberStyle: NumberFormatter.Style, decimals: Int, withThousandSeparator: Bool) -> String
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = numberStyle
numberFormatter.maximumFractionDigits = decimals
if !withThousandSeparator
numberFormatter.groupingSeparator = ""
return numberFormatter.string(from: NSNumber(value: self)) ?? ""
func textFieldDidBeginEditing(_ textField: UITextField)
if textField === self.textField
textField.text = amount.doubleToString(numberStyle: .decimal, decimals: 2, withThousandSeparator: false)
ios swift uitableview swift3 uitextfield
I have a UItextField
called textField
on a static cell tableView. That field contains a currency amount - like "$10,000.00".
When editing that amount, the currency and thousand grouping symbols are a bit in the way. Therefore I would like to remove those when the field becomes the first responder.
I do this in textFieldShouldBeginEditing
.
The first time I do this, everything works. The textField
's contents are reformatted without currency and thousand group separator.
On textFieldDidEndEditing
I re-format the value into a proper currency string again. This also works.
The problem happens when I re-enter the field for a 2nd time. While debugging I can see the textField.text
has changed into the string without currency symbol and grouping symbol, but the display does not show that. Although it did work the first time! The 2nd time it looks like there is a mismatch between what is on the screen and what value the debugger sees.
I've tried things like:
- tableView.beginUpdates(); tableView.endUpdates()
- textView.setNeedsDisplay
... but this does not work.
So I copied the code that removes the currency formatting in textFieldShouldBeginEditing
to a new delegate method textFieldDidBeginEditing
.
Then everything worked fine. I could tap other controls and back to the textField a number of times and each time the control would lose its formatting when entered and would be restored to a formatted currency string after losing focus.
So I decided to delete the method textFieldShouldBeginEditing
. But then things broke down again! It looks like I have to implement both textFieldShouldBeginEditing
as well as textFieldDidBeginEditing
to be able to prepare a textField
's contents for user-editing?
Is this a bug?
extension Double
public func doubleToString(numberStyle: NumberFormatter.Style, decimals: Int, withThousandSeparator: Bool) -> String
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = numberStyle
numberFormatter.maximumFractionDigits = decimals
if !withThousandSeparator
numberFormatter.groupingSeparator = ""
return numberFormatter.string(from: NSNumber(value: self)) ?? ""
func textFieldDidBeginEditing(_ textField: UITextField)
if textField === self.textField
textField.text = amount.doubleToString(numberStyle: .decimal, decimals: 2, withThousandSeparator: false)
ios swift uitableview swift3 uitextfield
ios swift uitableview swift3 uitextfield
edited Nov 15 '18 at 19:35
Arjen Hiemstra
asked Nov 15 '18 at 19:26
Arjen HiemstraArjen Hiemstra
897
897
What do you do in textFieldShouldBeginEditing ? Just return true ?
– claude31
Nov 15 '18 at 19:57
Would you mind adding the code to reproduce the issue?
– Kamran
Nov 15 '18 at 22:37
add a comment |
What do you do in textFieldShouldBeginEditing ? Just return true ?
– claude31
Nov 15 '18 at 19:57
Would you mind adding the code to reproduce the issue?
– Kamran
Nov 15 '18 at 22:37
What do you do in textFieldShouldBeginEditing ? Just return true ?
– claude31
Nov 15 '18 at 19:57
What do you do in textFieldShouldBeginEditing ? Just return true ?
– claude31
Nov 15 '18 at 19:57
Would you mind adding the code to reproduce the issue?
– Kamran
Nov 15 '18 at 22:37
Would you mind adding the code to reproduce the issue?
– Kamran
Nov 15 '18 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
You should try textField's text changed event, attaching code below :
Add target on textField for text changed :
self.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Function for textField's text change :
func textFieldDidChange(_ textField: UITextField)
if textField === self.textField
//try formatting here
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326641%2funexpected-behaviour-text-textfieldshouldbeginediting-textfielddidbeginediting%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should try textField's text changed event, attaching code below :
Add target on textField for text changed :
self.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Function for textField's text change :
func textFieldDidChange(_ textField: UITextField)
if textField === self.textField
//try formatting here
add a comment |
You should try textField's text changed event, attaching code below :
Add target on textField for text changed :
self.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Function for textField's text change :
func textFieldDidChange(_ textField: UITextField)
if textField === self.textField
//try formatting here
add a comment |
You should try textField's text changed event, attaching code below :
Add target on textField for text changed :
self.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Function for textField's text change :
func textFieldDidChange(_ textField: UITextField)
if textField === self.textField
//try formatting here
You should try textField's text changed event, attaching code below :
Add target on textField for text changed :
self.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Function for textField's text change :
func textFieldDidChange(_ textField: UITextField)
if textField === self.textField
//try formatting here
answered Nov 16 '18 at 10:41
Shruti ThombreShruti Thombre
8311823
8311823
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326641%2funexpected-behaviour-text-textfieldshouldbeginediting-textfielddidbeginediting%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you do in textFieldShouldBeginEditing ? Just return true ?
– claude31
Nov 15 '18 at 19:57
Would you mind adding the code to reproduce the issue?
– Kamran
Nov 15 '18 at 22:37