我在核心数据中有 2 个实体来创建倒数计时器。Timer
有一个名为的属性timerName
,实体Blinds
(从“Times”更改)有一个名为 的属性duration
。
实体调用
Timer <---->> Blind
和属性称为
timerName <---->> duration
关系称为
blinds <---->>timer
我需要一次将各种持续时间放入倒数计时器中。当第一个持续时间达到 0 时,从核心数据中获取下一个持续时间,并将其倒数到零,等等。
我对 Objective-C 和核心数据非常陌生,但我知道我需要一个循环并获取请求,但不知道从哪里开始。任何代码示例将不胜感激。谢谢
编辑
我在我的 model.m 中设置了一个 fetchrequest
- (NSFetchedResultsController *)frc_newTimer
{
if (_frc_newTimer) return _frc_newTimer;
// Otherwise, create a new frc, and set it as the property (and return it below)
_frc_newTimer = [_cdStack frcWithEntityNamed:@"Timer"
withPredicateFormat:nil
predicateObject:nil
sortDescriptors:@"timerName,YES"
andSectionNameKeyPath:nil];
return _frc_newTimer;
}
然后在我的视图中controller.h
#import <UIKit/UIKit.h>
#import "Timer.h"
#import "Blind.h"
@interface BlindTimerViewController : UIViewController <NSFetchedResultsControllerDelegate>
{
IBOutlet UILabel *lblCountDown;
NSTimer *countdownTimer;
int secondsCount;
}
- (IBAction)StartTimer:(id)sender;
- (IBAction)ResetTimer:(id)sender;
@property (assign, nonatomic) NSInteger currentTimeIndex;
@property (nonatomic, strong) Model *model;
@property (nonatomic, strong) Timer *myTimer;
@end
然后在视图 controller.m
@interface BlindTimerViewController ()
@end
@implementation BlindTimerViewController
@synthesize model = _model;
和
-(void) timerRun
{
secondsCount = secondsCount -1;
int minutes = secondsCount / 60;
int seconds = secondsCount - (minutes * 60);
NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];
lblCountDown.text = timerOutput;
//need to add a label for the next blind in the coredata list and update it while in a loop......
if (secondsCount == 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void) setTimer{
// Configure and load the fetched results controller
self.model.frc_newTimer.delegate = self;
self.model.frc_newTimer.fetchRequest.predicate = [NSPredicate predicateWithFormat:@"timerName LIKE %@", @"Sample Timer"];
//add code to get the first coredata item in the blinds list
secondsCount = 240; // i need to insert the CoreData Blinds HERE
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
和按钮(尚未完全排序)开始操作
- (IBAction)StartTimer:(id)sender
{
[self setTimer];
}
- (IBAction)ResetTimer:(id)sender {
[countdownTimer invalidate];
countdownTimer = nil;
secondsCount = 0;
lblCountDown.text = @"00:00";
}