NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: Reset your "Label.text" after clicking a button

Forums > Swift

Hello guys, I made An app that one part shows a certain text after a button press, the issue is that when I click again on the same button the old text are still there - I want it to be cleared after the second press of the same button and show the new text - how do I do that???


Try creating a boolean variable which will tell the button action what to do. E.g in your button tapped function you can use that boolean to decide what to do -

create variable and set it to false by default. (outside of button tapped func)

inside button tapped func - if false then set your text and change bool to true

if true then clear the text and change bool to false.

Give it a try. Should work


Hello, first of all thank you for your reply!

  1. First click shows the text i want
  2. Second click clears the text
  3. when i click again it shows me the text from the first click - it dosent Clear it from the memory, this is the code:
var buttonWasPressed = false
    @IBAction func GenerateButton(_ sender: UIButton) {
        if buttonWasPressed == false {
        Label.text = "This is your password: \(password)"
            buttonWasPressed = true
        }else {
            Label.text = ""
            buttonWasPressed = false


Where have you declared your variable password and your variable number? I would have to look at your password generator code. That may be the issue there. I tried the following and it worked fine -

@IBOutlet weak var label: UILabel!
var buttonHasNoText: Bool = true
var password: Int!

  override func viewDidLoad() {
      // Do any additional setup after loading the view.

  @IBAction func buttonPressed(_ sender: Any) {
      if buttonHasNoText {
          label.text = "\(password!)"
          buttonHasNoText = false
      } else {
          label.text = ""
          buttonHasNoText = true

  func generateRandomNumber() {
      password = Int.random(in: 1...100)

Just a little tip with boolean conditions with if/else statements. You can just use what i have done above instead of the == false. With an if/else statement, saying 'if' then condition is liking saying 'if true'. If you want to check if a boolean variable is true then all you have to do is say 'if booleanVariableName'. If you want to check if a boolean variable is false (like in your code) then you have to use the 'NOT' operator before the boolean variable, like this - if !booleanVariableName. Just saves a little typing.


Sorry mate, also doesn't work, it deletes the the text entirely! i did manage to solve it like this:

@IBAction func GenerateButton(_ sender: UIButton) {
    Label.text = ""
    password = ""
    Label.text = "This is your password: \(password)"



Oops sorry mate i actually read your initial post wrong. i thought you wanted each press of the button to alternate between showing the password and clearing it. My bad. Glad you solved it :)


It's ok, thank you for helping 👍


Hacking with Swift is sponsored by Essential Developer

SPONSORED From January 26th to 31st you can join a FREE crash course for iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a senior developer!

Save your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.