我已经使用Interface Builder作为xib创建了一个视图。之所以这样做,是因为我希望为用户提供增加这些视图中任意数量的视图控制器的选项。我创建了一个Xib视图(名为“TimeRangeView.xib”,创建了一个自定义类(名称为“TimeRangeView.swift”),并将xib的类设置为该自定义类,将情节提要板元素连接到TimeRangeView.swift的IBOutlets,设置了xib的文件所有者添加到TimeRangeView.swift中,并在视图控制器中将其类设置为View的视图添加到我的TimeRangeView.swift中。
ios - 在 Storyboard 中使用Xib时,Xib的IBOutlets返回nil-LMLPHP

TimeRangeView.swift:

import UIKit

class TimeRangeView: UIView {
    @IBOutlet var startTimeLabel: UILabel!
    @IBOutlet var startDatePicker: UITextField!
    @IBOutlet var endTime: UILabel!
    @IBOutlet var endDatePicker: UITextField!
    @IBOutlet var addButton: UIButton!
    @IBOutlet var maxTimeLabel: UILabel!



    @IBAction func addButtonPressed(_ sender: UIButton) {

    }
}

我正在使用的情节提要UIViewController具有一个Interface Builder视图作为子视图;该视图的类分配为TimeRangeView.swift。该视图作为IBOutlet连接到视图控制器:
@IBOutlet var timeRangeView: TimeRangeView!

在视图控制器的viewDidLoad()方法中,我想更改一些TimeRangeView的子视图(如上所述,它们作为IBOutlet连接),但是当我尝试更改其某些属性时,它们返回nil:
override func viewDidLoad() {
    super.viewDidLoad()
    timeRangeView.maxTimeLabel.text = "Max duration: \(timeString ?? "n/a")"

    //Change the text fields' input views
    let startTimeDatePickerToolBar = UIToolbar().ToolbarPicker(mySelect: #selector(dismissPicker))

    let endTimeDatePickerToolBar = UIToolbar().ToolbarPicker(mySelect: #selector(dismissPicker))

    startTimeDatePicker.setDate(Date(timeIntervalSince1970: NSDate().timeIntervalSince1970 + 60), animated: false)

    endTimeDatePicker.setDate(Date(timeIntervalSince1970: NSDate().timeIntervalSince1970 + 120), animated: false)

    timeRangeView.startDatePicker.inputView = startTimeDatePicker
    timeRangeView.startDatePicker.inputAccessoryView = startTimeDatePickerToolBar
    timeRangeView.startDatePicker.delegate = self

    timeRangeView.endDatePicker.inputView = endTimeDatePicker
    timeRangeView.endDatePicker.inputAccessoryView = endTimeDatePickerToolBar
    timeRangeView.endDatePicker.delegate = self
}

timeRangeView不返回nil;它正在加载。但是,当尝试访问timeRangeView的子视图(startDatePicker,endDatePicker,maxTimeLabel)时,它们都返回nil。

我收到的错误是:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

最佳答案

发生这种情况是因为从未加载过xib

因此,您可以在这里执行以下操作:首先在viewController中添加一个简单的UIView,而不是TimeRangeView

@IBOutlet var timeRangeContainerView: UIView!

现在在viewDidLoad中添加通过使用获取xib
let timeRangeView = Bundle.main.loadNibNamed("TimeRangeView", owner: self, options: nil)?[0] as? TimeRangeView

并将其添加为timeRangeContainerView的子视图
override func viewDidLoad() {
    super.viewDidLoad()
    let timeRangeView = Bundle.main.loadNibNamed("TimeRangeView", owner: self, options: nil)?[0] as? TimeRangeView

    timeRangeView.frame = CGRect(origin: CGPoint.zero, size: self.timeRangeContainerView.frame.size)

    self.timeRangeContainerView.insertSubview(timeRangeView, at: 0)

    //  Add constraints
    timeRangeView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10).isActive = true
    timeRangeView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
    timeRangeView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    timeRangeView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
    timeRangeView.translatesAutoresizingMaskIntoConstraints = false


    //Add the rest of your code here
}


约束是可选的

09-17 18:58