I have a menu that is shared by some viewControllers. To avoid duplication I gave the menu its own nib file. I load the menu with this code snippet. The menu is loaded from file, but is not retained.
If I set the owner, the menu is retained but then I lose all the other outlets in my viewController.
How do you retain this menu in swift?
override func viewDidLoad()
{
super.viewDidLoad()
var xibItems : NSArray?
NSBundle.mainBundle().loadNibNamed("Menu", owner: nil, topLevelObjects: &xibItems)
if let xibMenu = xibItems?.firstObject as? NSMenu
{
self.popupButton.menu = xibMenu
println(xibMenu) //ok
}
}
edit:
Assigning the menu to a local variable (to keep a strong reference) doesn't help. But if I assign to a local variable and print it out, then it works. That is not really satisfactory because I don't want the println statement. Avoiding an extra local variable would be a plus too.
override func viewDidLoad()
{
super.viewDidLoad()
var xibItems : NSArray?
NSBundle.mainBundle().loadNibNamed("Menu", owner: nil, topLevelObjects: &xibItems)
if let xibMenu = xibItems?.firstObject as? NSMenu
{
self.popupButton.menu = xibMenu // popupButton is an IBOutlet
self.popupMenu = xibMenu //local variable
println(xibMenu) //ok
}
}