Home > Software design >  Swift How to make the previous label disappear and the new one to appear
Swift How to make the previous label disappear and the new one to appear

Time:10-03

I’m making my first project that is a truth or dare game but when I tap the button to print a Truth/dare the labels are getting stacked on each other. How do I make it that the previous label disappear and the new one is there alone. Thanks in advance



import UIKit
import PlaygroundSupport

public class ViewController: UIViewController {
    public override func loadView() {
        //adding baground
        super.loadView()
        let view = UIView()
        view.backgroundColor = .red
        self.view = view
        //dare button
        let dareButton = UIButton(frame: CGRect(x:300, y: 500, width: 100, height: 69))
        dareButton.layer.cornerRadius = 10
        dareButton.backgroundColor = #colorLiteral(red: 0.6352941393852234, green: 0.6470588445663452, blue: 0.886274516582489, alpha: 1.0)
        dareButton.setTitle("dare", for: UIControl.State.normal)
        dareButton.isUserInteractionEnabled = true
        dareButton.addTarget(self, action: #selector(dareButtonDidTap), for: .touchDown)
        view.addSubview(dareButton)
        self.view = view
        view.addSubview(dareButton)
    }
    @objc
    private func dareButtonDidTap() {
        let lbl = UILabel(frame: CGRect(x:95 , y:100, width: 325, height: 500))
        lbl.text = daresEng.randomElement()
        lbl.textColor = #colorLiteral(red: 0.6313725709915161, green: 0.6470588445663452, blue: 0.9058823585510254, alpha: 1.0)
        lbl.numberOfLines = 6
        view.addSubview(lbl)
    }
}

PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = ViewController()

CodePudding user response:

The labels are stacked because you create a new label and add it to the view each time you click the dareButton, and the previous labels are not removed from the superView. So you can move the label to the class level and add it to the view in loadView(). This way you only have one label, and in the dareButtonDidTap(), you only have to change the text of this label.

import UIKit
import PlaygroundSupport

public class ViewController: UIViewController {
    let lbl = UILabel(frame: CGRect(x:95 , y:100, width: 325, height: 500))
    
    public override func loadView() {
        //adding baground
        super.loadView()
        let view = UIView()
        view.backgroundColor = .red
        self.view = view
        //dare button
        let dareButton = UIButton(frame: CGRect(x:300, y: 500, width: 100, height: 69))
        dareButton.layer.cornerRadius = 10
        dareButton.backgroundColor = #colorLiteral(red: 0.6352941393852234, green: 0.6470588445663452, blue: 0.886274516582489, alpha: 1.0)
        dareButton.setTitle("dare", for: UIControl.State.normal)
        dareButton.isUserInteractionEnabled = true
        dareButton.addTarget(self, action: #selector(dareButtonDidTap), for: .touchDown)
        view.addSubview(dareButton)
        self.view = view
        
        view.addSubview(lbl)
        view.addSubview(dareButton)
    }
    
    @objc
    private func dareButtonDidTap() {
        lbl.text = daresEng.randomElement()
        lbl.textColor = #colorLiteral(red: 0.6313725709915161, green: 0.6470588445663452, blue: 0.9058823585510254, alpha: 1.0)
        lbl.numberOfLines = 6

    }
}

PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = ViewController()
  • Related