【问题标题】:Swift How to make the previous label disappear and the new one to appearSwift 如何使以前的标签消失并出现新的标签
【发布时间】:2021-11-23 08:47:34
【问题描述】:

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



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

【问题讨论】:

    标签: swift swift-playground


    【解决方案1】:

    标签是堆叠的,因为您每次单击dareButton 时都会创建一个新标签并将其添加到视图中,并且之前的标签不会从superView 中删除。因此您可以将标签移动到类级别并将其添加到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()
    

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 2019-01-02
      • 2019-07-23
      • 1970-01-01
      • 2019-10-11
      • 2014-03-23
      • 2022-11-18
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多