Swift如何让之前的标签消失,新的标签出现

Swift How to make the previous label disappear and the new one to appear

我正在制作我的第一个项目,这是一款真心话大冒险游戏,但是当我点击按钮打印 Truth/dare 时,标签会相互堆叠。我如何使以前的标签消失而新标签单独存在。提前致谢



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

标签是堆叠的,因为您每次单击 dareButton 都会创建一个新标签并将其添加到视图中,并且不会从 superView 中删除以前的标签。所以你可以将标签移动到class层级,并添加到loadView()层的视图中。这样你只有一个标签,在dareButtonDidTap()中,你只需要改变这个标签的文字即可。

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