UILabel不改变其值

我在用Swift做App开发中的简单练习。但不明白为什么这段代码不能工作,标签文本也没有改变。

import UIKit

class MiddleViewController: UIViewController {

    @IBOutlet var happened: UILabel!
    var eventNumber = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        if let existingText = happened.text {
            happened.text = "\(existingText) \n Event number \(eventNumber) was viewDidLoad"
            eventNumber += 1
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let existingText = happened.text {
            happened.text = "\(existingText) 1"
            eventNumber += 1
        }

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if let existingText = happened.text {
            happened.text = "\(existingText)\nEvent number \(eventNumber) was viewDidAppear"
            eventNumber += 1
        }

    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if let existingText = happened.text {
            happened.text = "\(existingText)\nEvent number \(eventNumber) was viewWillDisappear"
            eventNumber += 1
        }

    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if let existingText = happened.text {
            happened.text = "\(existingText)\nEvent number \(eventNumber) was viewDidDisappear"
            eventNumber += 1
        }

    }

}

事件如果我切换视图控制器,标签文本不会改变。

解决方案:

“(existingText)\nEvent编号(eventNumber)是viewWillDisappear”

因为在上面的文字中,你使用了”\n”,所以它将在标签的下一行显示文字,请检查你的标签的行数。如果是1,则不会显示,因为文字在第二行。

给TA打赏
共{{data.count}}人
人已打赏
解决方案

使用NSIS创建自定义页面

2022-5-13 15:00:12

解决方案

组件上使用复阶必须满足哪些要求?

2022-5-13 15:00:14

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索