NSManagedObjectContext Help
I'm just experimenting and trying to learn. I have a Simple view with 1 textbox, label and save button. When the button is pressed I want to save the data in the textbox to core data and update the label. Thanks
Inside DailyClinicalPerformanceRecord.m
- (IBAction)btnSave:(id)sender {
DailyClinicalPerformanceRecord* delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* managedObjectContext = delegate.managedObjectContext;
NSManagedObject* newForm;
newForm = [NSEntityDescription insertNewObjectForEntityForName:@"DCPR" inManagedObjectContext:managedObjectContext];
[newForm setValue:txtIncidentNum.text forKey:@"indidentNum"];
txtIncidentNum.text = @"";
NSError *error;
[managedObjectContext save:&error];
status.text = @"Form Saved";
}
Inside DailyClinicalPerformanceRecord.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface DailyClinicalPerformanceRecord : UIViewController<UIApplicationDelegate>{
UITextField *txtIncidentNum;
UILabel *status;
}
@property (nonatomic, retain) IBOutlet UITextField *txtIncidentNum;
@property 开发者_Python百科(nonatomic, retain) IBOutlet UILabel *status;
- (IBAction)btnSave:(id)sender;
- (IBAction)btnBack:(id)sender;
- (void)dismissKeyboard;
@end
I get error:
/Users/specked/Programs/EMTDocs/EMTDocs/DailyClinicalPerformanceRecord.m:67: error: request for member 'managedObjectContext' in something not a structure or union
And Warning
/Users/specked/Programs/EMTDocs/EMTDocs/DailyClinicalPerformanceRecord.m:66: warning: type 'id <UIApplicationDelegate>' does not conform to the 'NSCoding' protocol
It's a little hard to give a definitive answer to your question as you didn't give any details on what the problem is. But here's a few things to check:
- Check that you've made connections to the txtIncidentNum and status objects along with the btnSave method in Interface Builder
- Make sure that btnSave is being called (NSLog is handy)
- Make sure that managedObjectContext is not nil
Also, something is kind of fishy with this line:
DailyClinicalPerformanceRecord* delegate = [[UIApplication sharedApplication] delegate];
The fishy thing is that it's inside of DailyClinicalPerformanceRecord.m. If this code is in DailyClinicalPerformanceRecord.m and DailyClinicalPerformanceRecord is your delegate, you can get your managedObjectContext via self and not jump through these hoops. Or, it could be indicative of a larger problem and/or an organizational issue.
Hopefully one of those items will get you on track. If not, please expand your question with more details about what is and is not happening when you press the save button.
精彩评论