我已經爲Mac OSX創建了一個文檔應用程序來處理CoreDataModel。PersistentDocument之外的NSManagedObjectContext
現在我試圖以編程方式在單擊按鈕時將值保存在coredata中。
我能夠保存的值時,應用程序啓動:
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
NSManagedObjcetContext *moc = [self managedObjectContext];
NSSet *session = [moc fetchObjectsForEntityName:@"Sessions" withPredicate:nil];
NSLog(@"%d", (int)[session count]);
NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Sessions"
inManagedObjectContext:moc];
[obj setValue:@"TEST" forKey:@"name"];
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:112700] forKey:@"start"];
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:118700] forKey:@"stop"];
}
但我要救一個NSObject的內部計數器的值。所以我試圖在PersistentDocument中創建一個像前面傳遞值一樣的函數,但是coredata元素的計數是0,所以我認爲這不是引用正確的實體。
任何人都可以解釋我如何做到這一點或如何這可以工作?
感謝 啤酒
編輯:
我會試着更清楚。
我有一個帶START和STOP按鈕的計時器。我想用另一個按鈕SAVE保存計時器的值,當它停止時,在coredata中。計數器的管理位於NSObject CounterObject中。
我該怎麼辦?現在,我只能通過PersistentDocument從windowControllerDidLoadNib中調用函數來寫入核心數據。我想調用一個函數,在coredata中寫入我的計數器值,但如果我從CounterObject調用PersistentDocument中的函數,我插入的日誌顯示0個元素而不是4個,因此它沒有正確傳遞。
下面是代碼:
// Document
#import "Document.h"
#import "NSManagedObjectContext.h"
@implementation Document
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
[self saveTimeMOC:[NSDate dateWithString:@"11:23:34"]];
}
-(IBAction)saveTimeMOC:(NSDate *)time {
NSManagedObjectContext *moc = [self managedObjectContext];
NSSet *session = [moc fetchObjectsForEntityName:@"Sessions" withPredicate:nil];
NSLog(@"%d", (int)[session count]);
NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Sessions"
inManagedObjectContext:moc];
[obj setValue:@"TEST" forKey:@"name"];
[obj setValue:time forKey:@"start"];
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:118700] forKey:@"stop"];
}
+ (BOOL)autosavesInPlace
{
return YES;
}
@end
// CounterObject
#import "CounterObject.h"
#import "Document.h"
@implementation CounterObject
@synthesize contText, startButton, stopButton;
-(id)init {
self = [super init];
if (self) {
}
return self;
}
- (IBAction)startContatore:(id)sender {
stopButton.title = @"Stop";
[stopButton setEnabled:YES];
[startButton setEnabled:NO];
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(gestioneTimer) userInfo:nil repeats:YES];
}
- (IBAction)stopContatore:(id)sender {
if (timer != nil) {
[timer invalidate];
timer = nil;
}
if (stopButton.title != @"Reset") {
[startButton setEnabled:YES];
stopButton.title = @"Reset";
startButton.title = @"Continue";
} else if (stopButton.title == @"Reset") {
stopButton.title = @"Stop";
[stopButton setEnabled:NO];
startButton.title = @"Start";
timerCont = 0;
contText.stringValue = [NSString stringWithFormat:@"00:00"];
}
}
- (void)gestioneTimer {
timerCont += 1;
int minutes = floor(timerCont/60);
int seconds = trunc(timerCont - minutes * 60);
contText.stringValue = [NSString stringWithFormat:@"%02i:%02i", minutes, seconds];
}
- (IBAction)saveTime:(id)sender {
Document *moc = [[Document alloc] init];
[moc saveTimeMOC:[NSDate dateWithString:@"13:45:22"]];
}
你的問題不清楚 – samir
我會盡量做得更清楚。 – stinkokenzo
到目前爲止,您很難理解您遇到的問題。你會發布CounterObject用於執行更新的非工作代碼嗎? – paulmelnikow