gpt4 book ai didi

swift - 在 UIViewController 中创建新的 UIView

转载 作者:行者123 更新时间:2023-11-30 13:51:58 26 4
gpt4 key购买 nike

我有一个 ViewController,我用它来加载 UIView (TempView) 作为 subview 。

tempView = TempView(frame: CGRectMake(10, 10, view.frame.size.width, view.frame.size.height))
tempView.tempLabelText = "YAY"
view.addSubview(tempView)

tempView 有一个变量和一个 @IBOutlet

var tempLabelText : String?
@IBOutlet weak var tempLabel: UILabel!

我在 TempView 类的 awakeFromNib() 内添加了一条语句 print(tempLabelText) 及其打印 nil

我希望能够将 ViewController 的值更改为 tempLabelText,因此当我添加 subview 时,标签将显示我声明的内容。我错过了什么?

我的项目中的文件:

1.ViewController - 创建 tempView :TempView!

2.TempView.swift - UIView 类

3.tempView.xib - .xib 内有一个标签。连接到 TempView.swift 的标 checkout 口。

最佳答案

awakeFromNib() 方法在您初始化 View 时被调用:

tempView = TempView(frame: CGRectMake(10, 10, view.frame.size.width, view.frame.size.height))

此时变量tempLabelText尚未初始化。您仅在下一行为此变量赋值。

关于swift - 在 UIViewController 中创建新的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34151171/

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