我们可以删除以编程方式迅速添加的标签吗?

时间:2018-09-07 18:17:43

标签: ios swift uilabel programmatically-created

我目前在开发应用程序时遇到困难。为简单起见,它是一个允许您进行心理计算的应用程序。

我的问题是建议的计算结果,球员提供的结果以及计算结果的显示。实际上,当程序知道没有更多的计算可提出时,它将在包含for循环的函数的“情况2”中启动,如果循环提供的结果调用CreatLabelJuste()播放器是正确的,如果计算错误,则使用CreatLabelFAux()函数。

这些函数创建标签。效果很好。问题是要重启游戏,我调用viewDidLoad()函数来重置游戏。但是,当必须重新创建标签时,旧标签并没有将其从包含它们的视图中删除!

我不知道它是否足够清楚,但是如果有人知道是否有可能“销毁”这些标签,那么当游戏再次开始时,最后没有重叠。

func creatLabelJuste(TagX : Int) {
    let labelJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    let labelForAnswerJ =  UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    let  labelForResultJ =  UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))

    switch TagX {
    case 0 :
        labelJ.font = UIFont(name: "futura", size: 20)
        labelJ.center = CGPoint(x: 96, y: yAxis)
        labelJ.textAlignment = .center
        labelJ.text = "\(memoriseCalcule[compter])³"

        self.myView.addSubview(labelJ)

        labelForAnswerJ.font = UIFont(name: "futura", size: 20)
        labelForAnswerJ.center = CGPoint(x: 187, y: yAxis)
        labelForAnswerJ.textAlignment = .center

        labelForAnswerJ.text = "\(memoriseAnswer[compter])"
        self.myView.addSubview(labelForAnswerJ)

        labelForResultJ.font = UIFont(name: "futura", size: 20)
        labelForResultJ.center = CGPoint(x: 278, y: yAxis)
        labelForResultJ.textAlignment = .center
        labelForResultJ.text = "\(memoriseResult[compter])"
        self.myView.addSubview(labelForResultJ)
    //      print("/////////////////////////////////////CreateLabelJuste : LabelJ = \(labelJ.text)/ laberForAwnswerJ = \(labelForAnswerJ.text!)/ labelforResultJ+ = \(labelForResultJ.text) ////////////////////////////////////////////")
    case 1 :
        self.myView.removeFromSuperview()
    default :
        print("defaut")
    }
}

func creatLabelFaux(Tag : Int) {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    let labelForAnswer =  UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    let labelForResult =  UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    label.text = ""
    labelForResult.text = ""
    labelForAnswer.text = ""

    switch Tag {
    case 0:
        label.font = UIFont(name: "futura", size: 20)
        label.center = CGPoint(x: 96, y: yAxis)
        label.textAlignment = .center
        label.text = "\(memoriseCalcule[compter])³"
        label.backgroundColor = UIColor(red: 238/255, green: 105/255, blue: 86/255, alpha: 1)
        label.layer.cornerRadius = 10
        label.clipsToBounds = true
        self.myView.addSubview(label)

        labelForAnswer.font = UIFont(name: "futura", size: 20)
        labelForAnswer.center = CGPoint(x: 187, y: yAxis)
        labelForAnswer.textAlignment = .center
        labelForAnswer.backgroundColor = UIColor(red: 238/255, green: 105/255, blue: 86/255, alpha: 1)
        labelForAnswer.text = "\(memoriseAnswer[compter])"

        self.myView.addSubview(labelForAnswer)

        labelForResult.font = UIFont(name: "futura", size: 20)
        labelForResult.center = CGPoint(x: 278, y: yAxis)
        labelForResult.textAlignment = .center
        labelForResult.backgroundColor = UIColor(red: 238/255, green: 105/255, blue: 86/255, alpha: 1)
        labelForResult.text = "\(memoriseResult[compter])"
        labelForResult.layer.cornerRadius = 10
        labelForResult.clipsToBounds = true
        self.myView.addSubview(labelForResult)
    case 1 :
        //  label.delete(true)
        //labelForAnswer.delete(true)
        //labelForResult.delete(true)
        self.myView.removeFromSuperview()
    default:
        print("merde")
    }
}

for _ in 0..<setLimite{
    yAxis = yAxis + 30

    if memoriseAnswer[compter] == memoriseResult[compter]{
        creatLabelJuste(TagX: 0)
    } else {//if memoriseAnswer[compter] != memoriseResult[compter] {
        creatLabelFaux(Tag: 0)
    }

    compter = compter + 1
}

1 个答案:

答案 0 :(得分:0)

您需要在类而不是函数中保留对标签的引用。这样您就可以使用

.removeFromSuperview()删除标签,就像在这里一样:

self.myView.removeFromSuperview()

类似这样的东西:

var labelJ: UILabel!
var labelForAnswerJ: UILabel!
var  labelForResultJ: UILabel!

func creatLabelJuste(TagX : Int) {
    labelJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    labelForAnswerJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    labelForResultJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
    ...
    ...
    ...
}

此外,如果您手动拨打viewDidLoad()-这不是一个好方法

只需在游戏重启逻辑中添加一些新功能,然后从viewDidLoad()调用它

,当您需要重新启动游戏时,只需调用此函数即可代替viewDidLoad() 还为每个需要删除的标签添加label.removeFromSuperview()

并在需要重新启动游戏时致电restart()

将代码更改为此,替换myView初始化 同时更改viewDidLoad()Replay()。添加reset()函数

var myView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    reset()
}

func reset() {
     myView = UIView(frame: (CGRect(x: 0, y: 200, width: 375, height: 400)))

    self.enterTextField.becomeFirstResponder()

    // is Hidden ppt
    ReplayBtn.isHidden = true
    goBackBtn.isHidden = true
    enterTextField.isHidden = false
    nextBtn.isHidden = false

    // set variables to 0
    numberToCube = 0
    result = 0
    resultFromUser = ""
    lenghtAnswer = 0
    stats = 0
    score = 0
    limite = 0
    scoreLbl.text = ""
    displayNumberLbl.text = ""
    displayResultLbl.text = ""
    displayAnswerLbl.text = ""
    memoriseCalcule.removeAll()
    memoriseAnswer.removeAll()
    memoriseResult.removeAll()
    ListeNombre = "Asked :\n"
    ListeAnswer = "Answer :\n"
    ListeCanswer = "Correct :\n"
    yAxis = 50

    compter = 0

    // Label Manager
    scoreLbl.textColor = UIColor.black
    NumberLbl.textColor = UIColor.black
    scoreLbl.textAlignment = NSTextAlignment.center
    self.NumberLbl.frame = CGRect(x: 26, y: 100, width: 322, height: (50))
    self.scoreLbl.frame = CGRect(x: 26, y: 180, width: 322, height: (50))
    NumberLbl.textColor = UIColor.white
    // Launch func

    number()
}

@IBAction func Replay(_ sender: Any) {
    LabelJ.removeFromSuperview()
    labelForAnswerJ.removeFromSuperview()
    labelForResultJ.removeFromSuperview()

    reset()
    creatLabelFaux(Tag: 1)
    creatLabelJuste(TagX: 1)
}