Here's 2 binding issues I'd like to share with you, related to selector name and parameters when configured in IB.
I have CoreData
objets retrieved through an ArrayController
and populated in a NSTableView
. All the binding is made through Interface Builder. Each row contains a button that should check an url (one of the properties of the entity).
1. To make the button fire, I first tried to bind it this way:
For the target:
- Bind to: File's owner (in this case, a
ViewController
) - Model Key Path:
self
- Selector Name:
revealInFinder:
For the argument:
- Bind to:
Table Cell View
- Model Key Path: ObjectValue
- Selector Name:
revealInFinder: (<automatically set>)
When I click the button, the function revealInFinder(object: NSManagedObject)
that I wrote in the ViewController
is simply never called: I tried with or without argument, with or without a colon, nothing happens: no breakpoint triggered, no message in the console.
I can put random chars in the selector name, I don't even get a
"unrecognized selector name"!
... Same thing if I change the name of the function in the ViewController
, or simply remove it: Nothing happens and no errors in console (of course, I've checked: the owner of the File is set to be the ViewController
, but the view wouldn't show up anyway).
As the binding are made in IB and not in code, it's difficult to trace and debug my mistakes (I also put -NSBindingDebugLogLevel 1 to the “Arguments to be passed at launch”, as recommended here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/Troubleshooting.html). But how am I supposed to handle something that is apparently a "non-event"?? Sure I'm missing something here but what?
2-I've decided to bind the button in a different way:
For the target: (without argument at first)
- Bind to: Table Cell View
- Model Key Path: ObjectValue (so the Core Data's entity, right?)
- Selector Name: revealInFinder:
This works, as I created NSManagedbject
subclasses for my entities, and wrote extensions for them.
The problem occurs when I add an argument, as I need to get the managedObjectContext to do some fetching inside the function:
For the argument:
- Bind to: File's owner
- Model Key Path: self.managedContext (the moc is set in the viewController)
After a bunch of "unrecognized selector sent" messages, I managed to write this function (not really sure why it's only called with the "_"):
func revealInFinder(_ managedContext: NSManagedObjectContext)
but the passed argument remains 0x00000000... (<uninitialized> in the console)
.
I changed the Model Key Path by removing the self., and then bound it to the managedObjectContext
of the ArrayController
instead):
- Bind to: Array Controller
- Controller Key: ArrangedObjects
- Model Key Path: managedObjectContext
The moc still remains uninitialized.
Why is my first try not triggering any action?
Why the argument is uninitialized in the second try (not even "nil")?