ios - 如何在 iOS 中将字符串变量分配给标签或文本字段

标签 ios swift string

当从 viewDidLoad 函数调用 print(mess) 时,它会打印控制台中预期的内容,但是当它被分配给 UITextFieldUILabel,则分配的值不会反射(reflect)在 UITextFieldUILabel 中。这是为什么?

这样做:

var mess: String = ""
override func viewDidLoad()
{
    super.viewDidLoad()
    self.infoLabel.text = mess //label not getting updated

    print(mess) //printing the required output in console
}

但是当我这样做时它工作正常

override func viewDidLoad()
{
    super.viewDidLoad()
    let date = DATE()
    let calendar = Calendar.current
    let minutes = calendar.component(.minutes, from: date)
    temp.text = String(minutes)


}

最佳答案

首先为困惑变量赋值,然后更新 UITextField 值。 在 viewDidLoad 中为 UITextField 赋值时,其具有默认值 ""

var mess: String = ""
    override func viewDidLoad()
    {
        super.viewDidLoad()
        mess = "123"
        self.infoLabel.text = mess //label not getting updated

        print(mess) //printing the required output in console
    }

关于ios - 如何在 iOS 中将字符串变量分配给标签或文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49167170/

相关文章:

objective-c - Swift:控制台中的 Print(object) 不显示变量属性,例如 NSObjects 的 Objective-C

c# - 如何改变 C# 中数字的文化?

iphone - 获取地址簿数据,它的代码在模拟器上运行但没有运行 iPhone 设备。

ios - 将 Firestore DB 与 swift 4.0 一起使用,每次我进行身份验证时,保存到数据库都会创建一个随机字 rune 档

ios - 如何禁用快速触摸和双开 View ?

ios - removeConstraint() 不删除约束

python - 向后递归打印字符串,每次递增

c++ - 类型 'float'和 'const c'的无效操作数

ios - 从 CMSampleBuffer 播放音频

php - ApnsPHP-发送推送通知时出错