Background: I built an app w/ inexperience. It uses bindings, a NSArrayController subclass and Core Data/iCloud. The experience level caused a lot of code unnecessary writing, yet the app worked ok. A button linked to the NSArrayController add:
method triggered newObject
and addObject:
. Removing never worked and led me to look at contentArray
binding . A week ago, I began cleaning the app to more thoroughly use the simplicity of bindings. (Unfortunately, complexity also entered into this problem.)
Assumptions: 1) add:
and remove:
methods would automagically fire the methods to create and delete objects. Many simple tutorials seem to work this way. 2) The add:
, addObject:
, remove:
and removeObject:
methods do not need to be overridden.
Problem: addObject:
and removeObject:
do not get called (after some point of change) unless they are explicitly called in the add:
or remove:
method. I have never had the functionality of deleting both the array controller and managed object with a simple click on a remove button.
Question: What is required for the Core Data managed objects to be added and removed?
Other info: The array controller pictured below has Custom Class set to CheckinArrayController. The Core Data stack is initialized in AppDelegate. The newObject
method has been overridden to preset some attributes.
EDIT: Override in array controller subclass add:
, newObject
, addObject:
, and arrangeObjects:
Code in Array Controller (NSArrayController subclass)
@implementation CheckinArrayController
- (id)newObject {
id newObject = [super newObject];
// do object set up here ....
return newObject;
}
- (void)add:(id)sender {
/* without the next 2 no object is added, although this is called
NSManagedObject *newItem = [self newObject];
[self addObject:newItem]; */
}
- (void)addObject:(id)object {
[super addObject:object];
}
- (void)remove:(id)sender {
[super remove:sender];
}
- (void)removeObject:(id)object {
[super removeObject:object];
}
@end
Current array controller connections:
Selection Indexes
of the table view to the array controller, you can bindenabled
of the buttton tocanRemove
of the array controller. Same for the add button andcanAdd
. – Willeke