ios - 在 .xib 中的 UICollectionViewCell 中使用 IBOutlet 时出现 NSUnknownKeyException

标签 ios swift xcode xib

我一直在尝试使用来自 xib 的 IBOutlet 添加标签到自定义 UICollectionViewCell 类,该类将用作 UICollectionView 我在另一个 xib 中有,但是当我添加 socket 时,我在我创建的标签引用上得到一个 NSUnknownKeyException,没有 socket 正确引用单元格加载的内容但是我希望能够在单元格内操作标签。

这是我在父 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
   }
}

这是子 xib 类(UICollectionViewCell):

class CalendarDay: UICollectionViewCell
{
   @IBOutlet weak var dayLabel: UILabel!  
}

如果有助于查看,这是我的总体项目:https://github.com/CharlemagneVI/practice-calendar

最佳答案

您设置的类和 IBOutlet 连接错误...好吧,不太正确...

CalendarDay.xib中,File's Owner类应该是默认的NSObject:

enter image description here

它应该有任何联系:

enter image description here

单元格对象本身的类应该是CalendarDay:

enter image description here

并且是您建立联系的地方:

enter image description here

enter image description here

应该这样做:)

关于ios - 在 .xib 中的 UICollectionViewCell 中使用 IBOutlet 时出现 NSUnknownKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51498328/

相关文章:

swift - 完成处理程序完成前为真

ios - 如何在特定时间和日期添加日历事件?

ios - Swift:如何更改键盘背景和字母的颜色?

ios - Xcode 4.5 - 崩溃日志未符号化

ios - PerformFetch后fetchedObjects为nil,但tableView有数据

ios - 使用 Storyboard与以编程方式进行 iOS 开发的速度?

objective-c - 为 Mac OS X 编写自定义桌面通知,例如音量弹出窗口

iOS 11 在使用大标题时无法正常滚动到顶部

swift - 如何使用 Swift 通过 NSPopUpButton 访问和排序 NSTableView

ios - 退出应用程序导致错误 "Message from debugger: Terminated due to signal 9"