gpt4 book ai didi

ios - swift:在 awakeFromNib() 中展开可选值时意外发现 nil

转载 作者:行者123 更新时间:2023-11-29 01:17:04 24 4
gpt4 key购买 nike

我可能只是错过了一些东西,但对于我来说,我想不通。

我正在开发一个具有 MVC 结构的应用程序;我将所有需要的字段放在 Xib 上,但我依赖 View 来实际更改字体、大小等。

这是我的代码:

import Foundation
import UIKit

protocol ProductionWellChemistryFormTwoViewDelegate {

}

class ProductionWellChemistryFormTwoView: BaseUIView {

let fontSizeL : CGFloat = 18
let fontName : String = "Futura"
var delegate : ProductionWellChemistryFormTwoViewDelegate!

@IBOutlet weak var labelTitle: UILabel!
@IBOutlet weak var labelFieldCode: UILabel!
@IBOutlet weak var labelGasFlaskNo: UILabel!
@IBOutlet weak var labelDatePrepared: UILabel!
@IBOutlet weak var labelDateVacuumed: UILabel!
@IBOutlet weak var labelClickingSound: UILabel!
@IBOutlet weak var labelTimeStarted: UILabel!
@IBOutlet weak var labelTimeFinished: UILabel!
@IBOutlet weak var labelWHPStarted: UILabel!
@IBOutlet weak var labelWHPEnded: UILabel!
@IBOutlet weak var labelSPPStarted: UILabel!
@IBOutlet weak var labelSPPEnded: UILabel!
@IBOutlet weak var labelSPStarted: UILabel!
@IBOutlet weak var labelSPEnded: UILabel!
@IBOutlet weak var labelRemarks: UILabel!

@IBOutlet weak var textfieldFieldCode: UITextField!
@IBOutlet weak var textfieldGasFlaskNo: UITextField!
@IBOutlet weak var textfieldDatePrepared: UITextField!
@IBOutlet weak var textfieldDateVacuumed: UITextField!
@IBOutlet weak var textfieldTimeStarted: UITextField!
@IBOutlet weak var textfieldTimeFinished: UITextField!
@IBOutlet weak var textfieldWHPStarted: UITextField!
@IBOutlet weak var textfieldWHPEnded: UITextField!
@IBOutlet weak var textfieldSPPStarted: UITextField!
@IBOutlet weak var textfieldSPPEnded: UITextField!
@IBOutlet weak var textfieldSPStarted: UITextField!
@IBOutlet weak var textfieldSPEnded: UITextField!
@IBOutlet weak var textviewRemarks: UITextView!

@IBOutlet weak var switchClickSound: UISwitch!

override func awakeFromNib() {
self.labelTitle.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelFieldCode.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelGasFlaskNo.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelDatePrepared.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelDateVacuumed.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelClickingSound.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelTimeStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelTimeFinished.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelWHPStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelWHPEnded.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelSPPStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelSPPEnded.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelSPStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelSPEnded.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.labelRemarks.font = UIFont(name: self.fontName, size: self.fontSizeL)

self.textfieldFieldCode.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldGasFlaskNo.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldDatePrepared.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldDateVacuumed.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldTimeStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldTimeFinished.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldWHPStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldWHPEnded.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldSPPStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldSPPEnded.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldSPStarted.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textfieldSPEnded.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textviewRemarks.font = UIFont(name: self.fontName, size: self.fontSizeL)
self.textviewRemarks.layer.borderWidth = 1.0
self.textviewRemarks.layer.borderColor = UIColor.blackColor().CGColor
self.textviewRemarks.text = ""
}
}

当我读取 awakeFromNib() 的第一行时,代码抛出异常,无论那是什么(我尝试注释掉第一行,所以它转到了第二行)。

我总共这样做了十几次,所以我真的不确定发生了什么。我唯一能想到的不同之处是尝试通过进入 Xib 的源代码并重命名类 ProductionWellChemistryFormTwoViewer 来修复以前的 Interface Builder 文件中的未知类,一个错误命名的类,然后我现在将其重命名为我的类(没有过去的 View )。

如果我禁用 awakeFromNib() 中的所有内容,应用程序就会运行。

编辑:如果我更改 XIB 源代码中的以下行,该应用程序也可以正常运行,包括字体和所有内容:

<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="vYc-5l-gfD" userLabel="ContentView" customClass="ProductionWellChemistryFormTwoView" customModule="Integra_Geochemistry" customModuleProvider="target">

回到:

<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="vYc-5l-gfD" userLabel="ContentView" customClass="ProductionWellChemistryFormTwoViewer"  customModule="Integra_Geochemistry" customModuleProvider="target">

我在 Interface Builder 文件中得到一个 Unknown class _TtC20Integra_Geochemistry36ProductionWellChemistryFormTwoViewer。 错误,但至少它有效。

我将扩展我上面提到的情况,这是发生的事情:

  1. 我创建了 XIB。
  2. 我创建了 View ,错误地命名为 ProductionWellChemistryFormTwoViewer
  3. 将 XIB 的类设置为所述 View 。
  4. 发现错误的名字。
  5. 将名称更改为 ProductionWellChemistryFormTwoView。弹出一些错误。
  6. 手动将对 ProductionWellChemistryFormTwoViewer 的任何引用更改为 ProductionWellChemistryFormTwoView
  7. 注意到运行该应用会产生未知类错误,进入 XIB 源代码,删除 ProductionWellChemistryFormTwoViewer
  8. 然后我得到了上面的错误。

最佳答案

所以很可能您没有链接其中一个 IBOutlets。

有那么多的网点,要找出哪个网点会让人感到困惑,所以我建议您采纳@originaluser2 的建议并使用 IBCollections。

class ProductionWellChemistryFormTwoView: BaseUIView {
let fontSizeL : CGFloat = 18
let fontName : String = "Futura"
var delegate : ProductionWellChemistryFormTwoViewDelegate!

// By declaring the IBOutlet as an array it tells IB that its a collection
@IBOutlet var labelCollection: [UILabel]!
@IBOutlet var textFieldCollection: [UITextField]!

@IBOutlet weak var textviewRemarks: UITextView!

override func awakeFromNib() {
let font = UIFont(name: self.fontName, size: self.fontSizeL)
labelCollection.each{ $0.font = font }
textFieldCollection.each{ $0.font = font }

textviewRemarks.layer.borderWidth = 1.0
textviewRemarks.layer.borderColor = UIColor.blackColor().CGColor
textviewRemarks.text = ""
}
}

然后通过您的 xib 并根据需要连接所有 socket 。

关于ios - swift:在 awakeFromNib() 中展开可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045517/

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