I present a UINavigationController as a ModalViewController. The UINavigationViewController handles a segue of (navigated) UIViewControllers. With my first ModalViewController (MVC) I want to create a new instance of my NSObject which has several properties. This works.
Now in every VC I have a slider to change one of the several properties of my object. At the last VC I want to save the object.
I’m using CoreData (which already works when I work with all of the properties in one VC).
Is there an easy way to achieve this?
(I’m also using NSNotificationCenter.)
EDIT:
- In MVC1 I send a
NSNotificationtoDetailTableViewControllerby pressingNextin myUINavigationBar. MVC1 pushes to MVC2. -
When DTVC receives the
NSNotificationit doesself insertNewObject:(standard Apple CoreData method) This methods works, I see a new instance of the object in myUITableViewafter dismissing the MVControllers. -
MVC2 has an
UISliderwhich value I want to store in my object. So I did#import "Object.h", @property (nonatomic, weak) Object * detailItem, @sythesize detailItemand stored a certain value usingdetailItem.property = NSNumber initWithInt etc.. I pressNext. MVC2 pushes to MVC3. -
MVC3 has also
#imported ... and @property .... It does inviewDidLoad:NSLog(@"%i", [detailItem.property intValue]);and shows0instead ofmyInteger. What am I doing wrong?
EDIT:
Which changes are necessary to make this piece of code work?
In ModalViewController
- (void)setDetailItem:(Object *)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
In MasterViewController
- (void)insertNewObject
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:@"New Object" forKey:@"name"];
[self.firstAssistantViewController setDetailItem:(Object *)newManagedObject];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
Pass the new object along as a property to the other modal view controllers. The managed object contains a reference to the managed object context so you can save at any point.
So, for each modal view controller:
.h:
.m:
And before you present each view controller:
Configure your UI (set the slider values etc) on
viewWillAppear:like so:And update the managed object using the reverse when the slider value changes:
In the final view controller, if you wish to save there, you can do this: