0

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.

CaOs433
  • 1,137
  • 1
  • 11
  • 15
  • 1
    Why downvotes? What’s wrong with the question? – CaOs433 Mar 06 '19 at 04: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: https://stackoverflow.com/questions/7678469/should-iboutlets-be-strong-or-weak-under-arc – Zhang Mar 06 '19 at 05: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 07 '19 at 02: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 07 '19 at 14:12
  • @Zhang Ok, thanks for the clarification. – CaOs433 Mar 08 '19 at 02:13

3 Answers3

3

Small improvement:

textView.text = nil

MQLN
  • 2,292
  • 2
  • 18
  • 33
1

Try using textView.text = "". If that's not working it could be that you're using a placeholder. Try textView.placeholder = ""

Evan
  • 125
  • 7
0

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.

CaOs433
  • 1,137
  • 1
  • 11
  • 15