I have a class called AccountPanelController
that contains two variables: an IBOutlet to an NSTableView
, as well as a simple Int
. When I run my app, awakeFromNib:
prints that both the NSTableView
and the Int
have been initialized. From another file, I have created and initialized an instance of the class AcountPanelController
. Via a button connected to that other file, I call openPanel:
on the instance. At that point accountTable
does not exist but somehow number
does. In other words, when I initialize the instance of my class the Int
is being created, but the NSTableView
, an IBOutlet, is not. Why is this? I assume it is because accountTable
is an IBOutlet, and therefore will not reinitialize. What can I do about this? (Xcode Version 6.1.1 (6A2008a), Cocoa, Swift)
Thanks,
bigelerow
class AccountPanelController: NSObject, NSTableViewDataSource {
@IBOutlet weak var accountTable: NSTableView!
var number: Int = 5
override func awakeFromNib() {
println(accountTable) // prints "<NSTableView: 0x100512aa0>"
println(number) // prints "5"
}
func openPanel() {
println(number) // prints "5"
println(accountTable) // prints "nil"
accountTable.reloadData() // throws error "unexpectedly found nil while unwrapping an Optional value"
}
// NSTableView data source functions below...
}
Other File
@IBAction func buttonPressed(sender: AnyObject?) {
var instance = AccountPanelController()
instance.openPanel()
}
IB automatically creates instances of all objects with IBOutlets when their xib file is loaded. So, instead of creating my own, second, instance in another file:
var instance = AccountPanelController()
I simply made a reference to the instance IB had already made for me:
IBOutlet weak var IBInstance: AcountPanelController!
Unlike the second instance, this IB created version contained initialized IBOutlets. When I referenced AccountTable
it was not nil.