gpt4 book ai didi

ios - 如何在 Swift 中实现全局化?

转载 作者:可可西里 更新时间:2023-11-01 00:53:55 26 4
gpt4 key购买 nike

我在 viewDidLoad 函数中创建了一个 UILabel。然后,我有一个在 viewDidLoad 函数之外的函数。我需要能够访问 UILabel

我知道我可以在 ObjC 中使用 extern 来做到这一点,但在 Swift 中如何做到这一点?

最佳答案

根据您的需要,您可以使用如下的属性声明来完成:

yourViewController : UIViewController {

var label :UILabel? // your property

override func viewDidLoad {

super.viewDidLoad()

label = UILabel() // an example initialization
}

yourMethod {

label // here you can access your label as
}
}

就是这样。

关于ios - 如何在 Swift 中实现全局化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24109230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com