我一直试图用一个从XIB中的IBOutlet添加到一个自定义的UICollectionViewCell类,该标签被用作我们在另一个XB中使用的UICollectionView的单元格,但是当我添加这个出口时,我在我创建的标签引用上得到了一个NSUnknownKeyException。在没有出口参考的情况下,单元格的内容加载正确,但我希望能够操作单元格内的标签。这是我父母XIB类的内容:class Calendar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{ let nibName: String = "Calendar" let calendarDayNibName: String = "CalendarDay" let calendarReusableCellName: String = "CalendarCell" let calendarDaysLimit: Int = 35 var contentView: UIView? @IBOutlet var sundayLabel: UILabel! @IBOutlet var mondayLabel: UILabel! @IBOutlet var tuesdayLabel: UILabel! @IBOutlet var wednesdayLabel: UILabel! @IBOutlet var thursdayLabel: UILabel! @IBOutlet var fridayLabel: UILabel! @IBOutlet var saturdayLabel: UILabel! @IBOutlet var calendarDays: UICollectionView! required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) guard let view = self.loadViewFromNib() else { return } view.frame = self.bounds self.addSubview(view) contentView = view contentView?.isUserInteractionEnabled = true } override func awakeFromNib() { super.awakeFromNib() calendarDays.register(UINib(nibName: calendarDayNibName, bundle: nil), forCellWithReuseIdentifier:calendarReusableCellName) calendarDays.dataSource = self } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return calendarDaysLimit; } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: calendarReusableCellName, for: indexPath) configureCell(cell: cell) return cell } private func configureCell(cell: UICollectionViewCell) { //does nothing right now, placeholder for if configuring cell on //collection view load } private func loadViewFromNib() -> UIView? { let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: nibName, bundle: bundle) self.isUserInteractionEnabled = true return nib.instantiate(withOwner: self, options: nil).first as? UIView } public func loadCalendarDays(month: Int) { //todo: write day loading logic here }}下面是子对象类( >):class CalendarDay: UICollectionViewCell{ @IBOutlet weak var dayLabel: UILabel!}这是我的总体计划,如果它有助于看:https://github.com/CharlemagneVI/practice-calendar 最佳答案 您设置的类和IBOutlet连接错误。。。嗯,不太对。。。在CalendarDay.xib中,File's Owner的类应该是默认的NSObject:它不应该有任何联系:单元格对象本身的类应为CalendarDay:这就是你联系的地方:应该这样做:)关于ios - 在.xib中的UICollectionViewCell中使用IBOutlet时,NSUnknownKeyException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51498328/
10-13 03:43