I am stuck on something dumb. This app is an OSX core-data app. I'm calling an action sheet to play with dates. The date selected is correctly shown on the screen after the sheet is closed. However, I cannot figure how to save the date.
Any help appreciated. This is the code I'm working with in my AppDelegate.m:
NSDate *theDate = [self.purchaseDatePicker dateValue];
if (theDate)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *formattedDateString;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
formattedDateString = [formatter stringFromDate:theDate];
[self.purchaseDate setStringValue: formattedDateString];
self.item.purchaseDate = theDate;
[self.item setPurchaseDate:theDate];
[self.item setPurchaseDate:self.item.purchaseDate];
NSLog(#"theDate is %#", theDate); <-- this is fine.
NSLog(#"item.purchaseDate is %#", self.item.purchaseDate); <-- this returns NULL
NSError *error = nil;
NSManagedObjectContext *moc = _coreDataController.mainThreadContext;
if (![moc save:&error])
{
[[NSApplication sharedApplication] presentError:error];
}
}
}
I found the answers here very helpful, but this post really got me thinking. So today, the code looks like below, and it works great!!
1.
[itemArrayController setValue: [DatePicker1 dateValue] forKeyPath: #"selection.date1"];
2.
if ([itemArrayController valueForKeyPath: #"selection.date1"] == NULL) {
date1 = [NSDate date];
[datePicker1 setDateValue: date1];
} else {
[datePicker1 setDateValue: [itemArrayController valueForKeyPath: #"selection.date1"]];
date1 = [itemArrayController valueForKeyPath: #"selection.date1"];
}
Related
I have added following code in my application.It is working fine if [comps setYear:1] but if i change year value to 2 or greater than 2 code does not show me any error but also not adding any event in calender. This happens in iOS 7 only. But if I run the same code on iOS 6 , its working correctly & event gets added in calender successfully. Is there any restriction in iOS 7 for adding future events ?
-(void)addEvent{
EKEventStore *es = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
[self setEventForStore:es];
} else {
}
}];
} else {
BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized);
if (granted) {
[self setEventForStore:es];
} else {
}
}
}
-(void)setEventForStore:(EKEventStore*)store{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Event 4";
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
// comps.day =3650;
comps.day=5;
comps.hour=1;
comps.year=2;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
event.startDate = sevenDays;
NSDate *sevenDays1 = [event.startDate dateByAddingTimeInterval:60*60];;
// duration = 1 h
event.endDate = sevenDays1;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}
`
Whenever dealing with dates through components, please set them in the following fashion:
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:05];
[comps setMonth:01];
[comps setYear:2014]; //instead of adding a single digit number like so: 2
//followed by your code...
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
//...
UPDATE 1
You mentioned that your event is not being saved in the calendar when you slightly adjust the integer values in your NSDate's components. One quick way to see whats happening is by NSLogging your date. See what the ouput is before you try to add it to your calendar.
//Right after this below line
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
//Add the code here so we can see what `sevenDays` prints out.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:sevenDays];
NSLog(#"Seven Days date = %#", strDate);
I was trying to set up a uidatepicker for booking some events in the calendar.
I need to disable (hide) some particular dates in my UIDatePicker, for instance days when I'm out of town or on vacation.
Can you help me with this one?
Thanx a lot in advance.
Here is some code:
- (void)viewDidLoad
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"IT"];
[self.datePicker setLocale:locale];
NSString *min = #"20132015";
NSString *max = #"22132015";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"ddMMyyyy HH:mm"];
NSDate *datemin = [dateFormat dateFromString:min];
NSDate *datemax = [dateFormat dateFromString:max];
[self.view addSubview:datePicker];
datePicker.minuteInterval = 15;
datePicker.maximumDate = datemax;
datePicker.minimumDate = [NSDate date];
[datePicker addTarget:self action:#selector(disableDate) forControlEvents:UIControlEventValueChanged];
[datePicker addTarget:self
action:#selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)disableDate{
NSDate *pickedDate = datePicker.date; // Get current Date
NSDate *disabledDate = [?????;
if([pickedDate compare:disabledDate] == NSOrderedSame)
{
[datePicker setDate:disabledDate animated:YES];
}
}
-(void)getDisableDate
{
?????
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd.MM.yyyy HH:mm"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"IT"];
[self.datePicker setLocale:locale];
labelDate.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
NSLog(#"date:%#", datePicker.date);
}
UIDatePicker doesn't support ranges of dates. What I would do instead is to change the date whenever a bad date is selected, by using setDate:animated: to the nearest date that is selectable. Also, to show a message somewhere explaining why the selected date was changed.
Either that, or, write your own custom UIDatePicker using UIPickerView. Not too hard, I've done it before. The worst part is dealing with wrapping values (eg: 31 -> 1). Does involve more work, though.
Best solution is create custom UIDatePicker using UIPickerView it's too easy and in future you can do changes easily.
Check the link.
i have a tableview which is populated with NSFetchedResultsController.
On didSelectRowAtIndexPathi am updating the current row. The sorting here should not change. The difference in the selected row is, that i mark it as read. No title changes or something.
In iOS4 the row gets updated and still leave on his current place, it does not move to the end of section. Thats the right way.
in iOS5 the row gets also updated but it moves to the end of the Section. I want to prevent that. How can i do that? And why is that happening?
See my code:
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"EntitySetsCardsInbox" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *inboxPred = [NSPredicate predicateWithFormat:#"archived == 0 AND cardId != 0"];
[fetchRequest setPredicate:inboxPred];
if (sortString == nil) {
//sortString = [[NSString alloc] initWithString:#"sortingOrder"];
sortString = [[NSString alloc] initWithString:#"colorOrder"];
sortAsc = NO;
}
if ([sortString isEqualToString:#"cardTitle"]) {
NSSortDescriptor *sortDescriptor2 = [[[NSSortDescriptor alloc] initWithKey:sortString ascending:sortAsc selector:#selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor2, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
XLog(#"sortDescriptors: %#", sortDescriptors);
} else {
NSSortDescriptor *sortDescriptor2 = [[[NSSortDescriptor alloc] initWithKey:sortString ascending:sortAsc] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor2, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
XLog(#"sortDescriptors: %#", sortDescriptors);
}
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[...]
}
i think your sortdescriptor is changing when you updating your rows. add one more sortdescriptor to fix the list with it and check it again. in my case i have a very similar issue and it solved my problem. maybe yours, too.
I have been trying to do this on an app for a long time but I cannot get it to work, please could someone help me and post some code on how I would do it. What I need to happen is when the text in the UITextField equal the UILabel. Thanks
h. viewcontroller
#interface AlphabetSceneViewController : UIViewController {
UILabel *stopWatchLabel;
NSTimer *stopWatchTimer;
NSDate *startDate;
IBOutlet UILabel *wordToType;
IBOutlet UITextField *wordTyped;
and the m. view controller
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
}
- (IBAction)onStartPressed:(id)sender {
startDate = [NSDate date];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
}
- (IBAction)onStopPressed:(id)sender {
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
if ([wordToType.text isEqualToString:#"stop"]) {
}
}
Help much appreciated!
I figured out how to do it, I'll put up the code for anyone that wanted to know. It's only code for the .m file and the alert view is just a bit of extra code on the end to make it show an alert when with the time that the user had when the timer stops.
- (IBAction)stopTimer:(id)sender {
if([wordTyped.text isEqualToString:wordToType.text]){
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Nice Work" message:[NSString stringWithFormat:#"Good Job! You're time to type the alphabet was %#. Let's see if you can do better...", stopWatchLabel.text]
delegate:self cancelButtonTitle:#"Main Menu" otherButtonTitles: nil];
[alert show];
}
}
I have this method to add an event to iOS calendar:
-(IBAction)addEvent:(id)sender
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = nome;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY/MM/dd"];
event.startDate = [formatter dateFromString:startdate];
event.endDate = [formatter dateFromString:enddate];
event.allDay = TRUE;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"%#", err);
[formatter release];
}
I would like to test *err to pop up an alert view if the registration has been done successfully.
If it is *err is null: is it correct? If *err=null I have to pop up the alert view.
Can anybody confirm it?
Thanks
Yes this is easily done as EKEventStore's method - (BOOL)saveEvent:(EKEvent *)event span:(EKSpan)span error:(NSError **)error actually returns a YES or NO value depending on the result of the save.
So you could do something like:
if (![eventStore saveEvent:event span:EKSpanThisEvent error:&err])
{
// code for event not saved
} else {
// code for event saved
}