Clear the content of UITextView in SwiftShould IBOutlets be strong or weak under ARC?How do I size a UITextView to its content?How to lose margin/padding in UITextView?Placeholder in UITextViewUITextView text manipulationUITextView delegate methodsHow to call Objective-C code from Swift#pragma mark in Swift?Detecting tap on a UITextViewHow to limit the number of lines in UITextView?UITextView - Scrolling to selected place
Lowest total scrabble score
How should I respond when I lied about my education and the company finds out through background check?
Keeping a ball lost forever
What is going on with 'gets(stdin)' on the site coderbyte?
Does Doodling or Improvising on the Piano Have Any Benefits?
Plot of a tornado-shaped surface
Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
How much character growth crosses the line into breaking the character
How could a planet have erratic days?
Why is so much work done on numerical verification of the Riemann Hypothesis?
Non-trope happy ending?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Can disgust be a key component of horror?
How to hide some fields of struct in C?
How can I write humor as character trait?
Can a stoichiometric mixture of oxygen and methane exist as a liquid at standard pressure and some (low) temperature?
Limits and Infinite Integration by Parts
Does IPv6 have similar concept of network mask?
On a tidally locked planet, would time be quantized?
Can I visit Japan without a visa?
Why did the EU agree to delay the Brexit deadline?
Why does a simple loop result in ASYNC_NETWORK_IO waits?
Clear the content of UITextView in Swift
Should IBOutlets be strong or weak under ARC?How do I size a UITextView to its content?How to lose margin/padding in UITextView?Placeholder in UITextViewUITextView text manipulationUITextView delegate methodsHow to call Objective-C code from Swift#pragma mark in Swift?Detecting tap on a UITextViewHow to limit the number of lines in UITextView?UITextView - Scrolling to selected place
I have a UITextView
and a UIButton
in my app and I'm trying to get the text content of the UITextView
to be cleared when the UIButton
is tapped.
My code:
@IBOutlet weak var textView: UITextView!
@IBAction func ClearButtonTapped(_ sender: UIButton)
// I want to clear the text content of textView
Is there built-in function for that, in the UITextView
class? I didn't find anything when I searched the UITextView
class in Xcode.
My app is on Xcode 10.1 and Swift 4.2.
ios swift uitextview
add a comment |
I have a UITextView
and a UIButton
in my app and I'm trying to get the text content of the UITextView
to be cleared when the UIButton
is tapped.
My code:
@IBOutlet weak var textView: UITextView!
@IBAction func ClearButtonTapped(_ sender: UIButton)
// I want to clear the text content of textView
Is there built-in function for that, in the UITextView
class? I didn't find anything when I searched the UITextView
class in Xcode.
My app is on Xcode 10.1 and Swift 4.2.
ios swift uitextview
Why downvotes? What’s wrong with the question?
– CaOs433
Mar 6 at 4:07
Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
– Zhang
Mar 6 at 5:49
@Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
– CaOs433
Mar 7 at 2:28
1
Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
– Zhang
Mar 7 at 14:12
@Zhang Ok, thanks for the clarification.
– CaOs433
Mar 8 at 2:13
add a comment |
I have a UITextView
and a UIButton
in my app and I'm trying to get the text content of the UITextView
to be cleared when the UIButton
is tapped.
My code:
@IBOutlet weak var textView: UITextView!
@IBAction func ClearButtonTapped(_ sender: UIButton)
// I want to clear the text content of textView
Is there built-in function for that, in the UITextView
class? I didn't find anything when I searched the UITextView
class in Xcode.
My app is on Xcode 10.1 and Swift 4.2.
ios swift uitextview
I have a UITextView
and a UIButton
in my app and I'm trying to get the text content of the UITextView
to be cleared when the UIButton
is tapped.
My code:
@IBOutlet weak var textView: UITextView!
@IBAction func ClearButtonTapped(_ sender: UIButton)
// I want to clear the text content of textView
Is there built-in function for that, in the UITextView
class? I didn't find anything when I searched the UITextView
class in Xcode.
My app is on Xcode 10.1 and Swift 4.2.
ios swift uitextview
ios swift uitextview
edited Mar 6 at 22:52
CaOs433
asked Mar 5 at 23:00
CaOs433CaOs433
14529
14529
Why downvotes? What’s wrong with the question?
– CaOs433
Mar 6 at 4:07
Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
– Zhang
Mar 6 at 5:49
@Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
– CaOs433
Mar 7 at 2:28
1
Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
– Zhang
Mar 7 at 14:12
@Zhang Ok, thanks for the clarification.
– CaOs433
Mar 8 at 2:13
add a comment |
Why downvotes? What’s wrong with the question?
– CaOs433
Mar 6 at 4:07
Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
– Zhang
Mar 6 at 5:49
@Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
– CaOs433
Mar 7 at 2:28
1
Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
– Zhang
Mar 7 at 14:12
@Zhang Ok, thanks for the clarification.
– CaOs433
Mar 8 at 2:13
Why downvotes? What’s wrong with the question?
– CaOs433
Mar 6 at 4:07
Why downvotes? What’s wrong with the question?
– CaOs433
Mar 6 at 4:07
Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
– Zhang
Mar 6 at 5:49
Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
– Zhang
Mar 6 at 5:49
@Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
– CaOs433
Mar 7 at 2:28
@Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
– CaOs433
Mar 7 at 2:28
1
1
Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
– Zhang
Mar 7 at 14:12
Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
– Zhang
Mar 7 at 14:12
@Zhang Ok, thanks for the clarification.
– CaOs433
Mar 8 at 2:13
@Zhang Ok, thanks for the clarification.
– CaOs433
Mar 8 at 2:13
add a comment |
3 Answers
3
active
oldest
votes
Small improvement:
textView.text = nil
add a comment |
Try using textView.text = ""
. If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""
add a comment |
I didn't find a ready function to clear the text content of an UITextView
so I created this code to do that:
The UITextView
variable:
@IBOutlet weak var textView: UITextView!
Function to clear the UITextView
:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.selectAll(textView)
if let range = textView.selectedTextRange textView.replace(range, withText: "")
When the clear-button is tapped, the function checks is there any text in the UITextView
and if there is some, it will select all the text in the UITextView
and replace it with an empty String
.
EDIT:
There is also the simple way to do it, which, for some reason, didn't work when I tried it before (probably because the bugs in Xcode 10.1), but this way the user can't undo it, if they accidentally tap the clear button:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.text = ""
Or with extension:
extension UITextView
func clear()
self.text = ""
Call textView.clear()
when you want to clear the text.
3
Why nottextView.text = ""
?
– Sh_Khan
Mar 5 at 23:02
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
All the content
– CaOs433
Mar 5 at 23:06
This only@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?
– Sh_Khan
Mar 5 at 23:07
|
show 4 more comments
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%2f55012981%2fclear-the-content-of-uitextview-in-swift%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Small improvement:
textView.text = nil
add a comment |
Small improvement:
textView.text = nil
add a comment |
Small improvement:
textView.text = nil
Small improvement:
textView.text = nil
answered Mar 6 at 0:50
MQLNMQLN
1,6171926
1,6171926
add a comment |
add a comment |
Try using textView.text = ""
. If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""
add a comment |
Try using textView.text = ""
. If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""
add a comment |
Try using textView.text = ""
. If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""
Try using textView.text = ""
. If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""
answered Mar 6 at 1:31
EvanEvan
426
426
add a comment |
add a comment |
I didn't find a ready function to clear the text content of an UITextView
so I created this code to do that:
The UITextView
variable:
@IBOutlet weak var textView: UITextView!
Function to clear the UITextView
:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.selectAll(textView)
if let range = textView.selectedTextRange textView.replace(range, withText: "")
When the clear-button is tapped, the function checks is there any text in the UITextView
and if there is some, it will select all the text in the UITextView
and replace it with an empty String
.
EDIT:
There is also the simple way to do it, which, for some reason, didn't work when I tried it before (probably because the bugs in Xcode 10.1), but this way the user can't undo it, if they accidentally tap the clear button:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.text = ""
Or with extension:
extension UITextView
func clear()
self.text = ""
Call textView.clear()
when you want to clear the text.
3
Why nottextView.text = ""
?
– Sh_Khan
Mar 5 at 23:02
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
All the content
– CaOs433
Mar 5 at 23:06
This only@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?
– Sh_Khan
Mar 5 at 23:07
|
show 4 more comments
I didn't find a ready function to clear the text content of an UITextView
so I created this code to do that:
The UITextView
variable:
@IBOutlet weak var textView: UITextView!
Function to clear the UITextView
:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.selectAll(textView)
if let range = textView.selectedTextRange textView.replace(range, withText: "")
When the clear-button is tapped, the function checks is there any text in the UITextView
and if there is some, it will select all the text in the UITextView
and replace it with an empty String
.
EDIT:
There is also the simple way to do it, which, for some reason, didn't work when I tried it before (probably because the bugs in Xcode 10.1), but this way the user can't undo it, if they accidentally tap the clear button:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.text = ""
Or with extension:
extension UITextView
func clear()
self.text = ""
Call textView.clear()
when you want to clear the text.
3
Why nottextView.text = ""
?
– Sh_Khan
Mar 5 at 23:02
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
All the content
– CaOs433
Mar 5 at 23:06
This only@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?
– Sh_Khan
Mar 5 at 23:07
|
show 4 more comments
I didn't find a ready function to clear the text content of an UITextView
so I created this code to do that:
The UITextView
variable:
@IBOutlet weak var textView: UITextView!
Function to clear the UITextView
:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.selectAll(textView)
if let range = textView.selectedTextRange textView.replace(range, withText: "")
When the clear-button is tapped, the function checks is there any text in the UITextView
and if there is some, it will select all the text in the UITextView
and replace it with an empty String
.
EDIT:
There is also the simple way to do it, which, for some reason, didn't work when I tried it before (probably because the bugs in Xcode 10.1), but this way the user can't undo it, if they accidentally tap the clear button:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.text = ""
Or with extension:
extension UITextView
func clear()
self.text = ""
Call textView.clear()
when you want to clear the text.
I didn't find a ready function to clear the text content of an UITextView
so I created this code to do that:
The UITextView
variable:
@IBOutlet weak var textView: UITextView!
Function to clear the UITextView
:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.selectAll(textView)
if let range = textView.selectedTextRange textView.replace(range, withText: "")
When the clear-button is tapped, the function checks is there any text in the UITextView
and if there is some, it will select all the text in the UITextView
and replace it with an empty String
.
EDIT:
There is also the simple way to do it, which, for some reason, didn't work when I tried it before (probably because the bugs in Xcode 10.1), but this way the user can't undo it, if they accidentally tap the clear button:
@IBAction func ClearButtonTapped(_ sender: UIButton)
textView.text = ""
Or with extension:
extension UITextView
func clear()
self.text = ""
Call textView.clear()
when you want to clear the text.
edited Mar 8 at 2:12
answered Mar 5 at 23:00
CaOs433CaOs433
14529
14529
3
Why nottextView.text = ""
?
– Sh_Khan
Mar 5 at 23:02
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
All the content
– CaOs433
Mar 5 at 23:06
This only@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?
– Sh_Khan
Mar 5 at 23:07
|
show 4 more comments
3
Why nottextView.text = ""
?
– Sh_Khan
Mar 5 at 23:02
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
All the content
– CaOs433
Mar 5 at 23:06
This only@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?
– Sh_Khan
Mar 5 at 23:07
3
3
Why not
textView.text = ""
?– Sh_Khan
Mar 5 at 23:02
Why not
textView.text = ""
?– Sh_Khan
Mar 5 at 23:02
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
I tried but it didn't work
– CaOs433
Mar 5 at 23:04
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
Do you mean to clear a portion/selected or all the content ?
– Sh_Khan
Mar 5 at 23:05
All the content
– CaOs433
Mar 5 at 23:06
All the content
– CaOs433
Mar 5 at 23:06
This only
@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?– Sh_Khan
Mar 5 at 23:07
This only
@IBAction func ClearButtonTapped(_ sender: UIButton) textView.text = ""
doesn't work ?– Sh_Khan
Mar 5 at 23:07
|
show 4 more comments
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%2f55012981%2fclear-the-content-of-uitextview-in-swift%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
Why downvotes? What’s wrong with the question?
– CaOs433
Mar 6 at 4:07
Yeah, not sure why the down votes. As an extra check, can we assume you've connected your outlets correctly in Interface Builder? The other answers given by others below are good suggestions to clearing text. By the way, I believe your UITextView outlet should be strongly referenced not weakly referenced according to: stackoverflow.com/questions/7678469/…
– Zhang
Mar 6 at 5:49
@Zhang Why I should use Strong rather than Weak? There was a lot of opinions in the link you posted and Xcode uses Weak for default.
– CaOs433
Mar 7 at 2:28
1
Well, numerous people have cited Apple engineers recommending to use strong, one even mentioned performance benefits. As long as your app is working, whatever floats the boat I guess. I myself like to think of buttons defined inside a UIViewController as belonging to that controller, forming a single self contained coherent unit, so I maintain a strong reference to it. The thought of something weakly attached to my UIViewController feels like it's dangling on, gives me an uncomfortable feeling metaphorically speaking.
– Zhang
Mar 7 at 14:12
@Zhang Ok, thanks for the clarification.
– CaOs433
Mar 8 at 2:13