UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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???

2      

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

2      

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) {
        passwordGenerator(number)
        if buttonWasPressed == false {
        Label.text = "This is your password: \(password)"
            buttonWasPressed = true
        }else {
            Label.text = ""
            buttonWasPressed = false
        }
    }

2      

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() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.
  }

  @IBAction func buttonPressed(_ sender: Any) {
      generateRandomNumber()
      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.

2      

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 = ""
    passwordGenerator(number)
    Label.text = "This is your password: \(password)"

}

2      

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 :)

2      

It's ok, thank you for helping 👍

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.