NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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) {
        passwordGenerator(number)
        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() {
      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.

   

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

}

   

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 Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.