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
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Modeling an IP Address
"You are your self first supporter", a more proper way to say it
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
Why can't I see bouncing of a switch on an oscilloscope?
Which country benefited the most from UN Security Council vetoes?
Find the result of this dual key cipher
When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?
Approximately how much travel time was saved by the opening of the Suez Canal in 1869?
What's the point of deactivating Num Lock on login screens?
Why do I get two different answers for this counting problem?
What does the "remote control" for a QF-4 look like?
How do I draw and define two right triangles next to each other?
How do I gain back my faith in my PhD degree?
Intersection point of 2 lines defined by 2 points each
How to format long polynomial?
Can a vampire attack twice with their claws using Multiattack?
Is it possible to record a short contained sound no longer than 60 milliseconds?
Alternative to sending password over mail?
Can you really stack all of this on an Opportunity Attack?
Do infinite dimensional systems make sense?
Has there ever been an airliner design involving reducing generator load by installing solar panels?
Why does Kotter return in Welcome Back Kotter?
What are these boxed doors outside store fronts in New York?
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
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
15529
15529
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
15529
15529
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