Redraw TdCalendarView - iphone

In my alarm based application on iPhone,I am using TdCalenderView code by tinyfool.On that calendar I am calling setDayFlag:day in drawDateWords method for the day on which alarm is set.Tapping the date opens the list of alarms for the corresponding date.
Now,I am also using Tab bar in the same application.On clicking item 0 of tab bar I am showing view containing calendar and on clicking item 1 I am showing the view to write and save the note.
When I am writing a note and saving it,I want a point to be plotted on corresponding date.
But it doesn't happen.The point is not there.But when I tap once on the date,the point appears and on second tap the list is shown.
Here is my code:
appdelegate.m
calendar=[[TdCalendarView alloc]init];
View containing tab bar
this is switch case for tab item tags based on which I am showing and hiding the views
switch (tag) {
case 0:
WriteANoteView.hidden=YES;
RecordANoteView.hidden=YES;
BirthdayView.hidden=YES;
AllNotesView.hidden=NO;
app.calendar initCalView];
[app.calendar drawDateWords];
break;
TdCalendarView.m
-(void)initCalView{
currentTime=CFAbsoluteTimeGetCurrent();
currentMonthDate=CFAbsoluteTimeGetGregorianDate(currentTime,CFTimeZoneCopyDefault());
currentMonthDate.day=1;
currentSelectDate.year=0;
monthFlagArray=malloc(sizeof(int)*93);
[self clearAllDayFlag];
app=(Note_TakerAppDelegate *)[[UIApplication sharedApplication] delegate];
[app getWrittenNotesAndDates];
app.calendar.tableContentArray=[[NSMutableArray alloc]init];
app.calendar.tableRDatesArray=[[NSMutableArray alloc]init];
app.calendar.tableCrDatesArray=[[NSMutableArray alloc]init];
}
-(void)drawDateWords{
CGContextRef ctx=UIGraphicsGetCurrentContext();
int width=self.frame.size.width;
int dayCount=[self getDayCountOfaMonth:currentMonthDate];
int day=0;
int x=0;
int y=0;
int s_width=width/7;
int curr_Weekday=[self getMonthWeekday:currentMonthDate];
UIFont *weekfont=[UIFont boldSystemFontOfSize:12];
for(int i=1;i<dayCount+1;i++)
{
day=i+curr_Weekday-2;
x=day % 7;
y=day / 7;
NSString *date=[[[NSString alloc] initWithFormat:#"%2d",i] autorelease];
[date drawAtPoint:CGPointMake(x*s_width+15,y*itemHeight+headHeight)withFont:weekfont];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
for(int j=0;j<[app.datesofTextArray count];j++)
{
if([[NSString stringWithFormat:#"%#",[app.datesofTextArray objectAtIndex:j]] length] !=0)
{
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSDate *dateFromString=[dateFormatter dateFromString:[NSString stringWithFormat:#"%#",[app.datesofTextArray objectAtIndex:j]]];
[dateFormatter setDateFormat:#"dd"];
int day1=[[dateFormatter stringFromDate:dateFromString]intValue];
[dateFormatter setDateFormat:#"MM"];
int month1=[[dateFormatter stringFromDate:dateFromString]intValue];
[dateFormatter setDateFormat:#"yyyy"];
int year1=[[dateFormatter stringFromDate:dateFromString]intValue];
if(day1==day && month1==currentMonthDate.month && year1==currentMonthDate.year)
{
NSLog(#"in if rdate day1=%d,month1=%d,year1=%d",day1,month1,year1);
[self setDayFlag:day flag:1];
[self paintDot:day];
}
}
else
{
//NSLog(#"in else date=%#",[app.datesofTextArray objectAtIndex:i]);
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSDate *dateFromString=[dateFormatter dateFromString:[NSString stringWithFormat:#"%#",[app.creationDates objectAtIndex:j]]];
[dateFormatter setDateFormat:#"dd"];
int day1=[[dateFormatter stringFromDate:dateFromString]intValue];
[dateFormatter setDateFormat:#"MM"];
int month1=[[dateFormatter stringFromDate:dateFromString]intValue];
[dateFormatter setDateFormat:#"yyyy"];
int year1=[[dateFormatter stringFromDate:dateFromString]intValue];
if(day1==day && month1==currentMonthDate.month && year1==currentMonthDate.year)
{
NSLog(#"in if cdate day1=%d,month1=%d,year1=%d",day1,month1,year1);
[self setDayFlag:day flag:1];
[self paintDot:day];
}
}
}
[dateFormatter release];
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
}
}
I am not getting why this is happening.Any help would be greatly appreciated.

Ok.This may sound like a hack but it eventually worked.
I called [TdCalenderView moveNextMonth] before showing calendar view and [TdCalenderView movePrevMonth] after showing the calendar view with a flag to manage cancelling the animation when the method is invoked by Tab bar.It refreshed my calendar and issue is resolved.

Related

iPhone - How to import native calendar events to my iphone app?

I am doing one simple application using iPhone calendar, where I need to import the iPhone native calendar events into my iPhone app. How can I do this. I have a piece of code but it doesn't seems to be working. I have added some events into my iPhone native calendar. But when i retrieve it's not fetching anything. Here is the piece of code.
-(IBAction)importCalEvents:(id)sender
{
NSArray *caleandarsArray = [[NSArray alloc] init];
caleandarsArray = [[eventStore calendars] retain];
NSLog(#"Calendars from Array : %#", caleandarsArray);
for (EKCalendar *CalendarEK in caleandarsArray)
{
NSLog(#"Calendar Title : %#", CalendarEK.title);
}
}
you can fetch all the calendar events like this.
import these 2 framworks (add if not exists into your project).
1 EventKit/EventKit.h
2) EventKitUI/EventKitUI.h
call this function on button click
-(void)EventList{
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSDate *startDate = [[NSDate alloc] init];
NSDate *endDate = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:startDate];
NSArray *calendars = [eventStore calendars];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
endDate:endDate calendars:calendars];
NSArray *matchingEvents = [eventStore eventsMatchingPredicate:predicate];
EKEvent *anEvent;
NSLog(#"Total Events >> %d",[matchingEvents count]);
for (int j=0; j < [ matchingEvents count]; j++) {
anEvent = [matchingEvents objectAtIndex:j];
NSLog(#"Title >>>%#",anEvent.title);
NSLog(#"start date is %# \n End Date is >>> %#",anEvent.startDate,anEvent.endDate);
}
}
And set the startDate and EndDate as per your Requirement.
so you can get all the detatils you want.
Best Luck..

random BAD ACCESS when saving user defaults

I'm simply trying to save this array to the user defaults and it will crash at random. Sometimes it works, sometimes it gives me the EXC_BAD_ACCESS. Am I not releasing something properly?
- (void)setTextValue:(NSString *)valueText indexToSet:(NSUInteger)index
{
[self.pageData replaceObjectAtIndex:index withObject:valueText];
[[NSUserDefaults standardUserDefaults] setObject:self.pageData forKey:#"mynotes"];
}
Here is the method that i've determined is causing the errors. It was a method already created by Xcode that I added my own custom code to.
- (nbookDataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
nbookDataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"nbookDataViewController"];
if (self.pageData.count > 0 && index < self.pageData.count)
{
NSString *val = (NSString *)[self.pageData objectAtIndex:index];
dataViewController.dataObject = val;
}
else
{
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];
//[dateFormat release];
[self.pageData addObject:dateString];
dataViewController.dataObject = (NSString *)[self.pageData objectAtIndex:index];
}
dataViewController.myModel = (nbookModelController *)self;
dataViewController.dIndex = index;
//[self.mySaveData setObject:self.pageData forKey:#"mynotes"];
return dataViewController;
}
This tip will allow you code to break on the exception and let you check directly why this is happening:
https://stackoverflow.com/a/616526/46970

how i use iPhone calendar add event?

I want use iPhone calendar add Event.
I try ti.com.calendar module from github but in this module only save startTime EndTime, Title and details.
but, not use allDay repeat or not reminder.
How i use this. in calendar?
I also user notification for reminder. but, after delete event. the notification is not delete.
any suggestion is appreciated
bellow is peace of code. date for alarm is using to match work hours (users don't like to wakeup to do job :)
eventStore = [[EKEventStore alloc] init];
EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];
newEvent.calendar = eventStore.defaultCalendarForNewEvents;
NSString *titleForEvent = [NSString stringWithFormat:#"In country:%# will be:\n%# event",[mo valueForKey:#"name"],[mo valueForKey:#"necessaryData"]];
newEvent.title = titleForEvent;
newEvent.allDay = YES;
NSDate *date = [mo valueForKey:#"date"];
NSDate *dateAlarm = [mo valueForKey:#"dateAlarm"];
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
if (dateAlarm < [NSDate date]){
dateAlarm = [NSDate dateWithTimeIntervalSinceNow:+18000];
NSDateFormatter *dateForm = [[NSDateFormatter alloc]init];
[dateForm setDateFormat:#"%HH"];
NSString *hourOfAlarm = [dateForm stringFromDate:dateAlarm];
[dateForm release];
NSNumberFormatter *numberForm = [[NSNumberFormatter alloc] init];
NSNumber *hour = [numberForm numberFromString:hourOfAlarm];
[numberForm release];
int difference = 0;
if ([hour intValue] < 9) difference = (9 - [hour intValue]) *3600;
if ([hour intValue] > 17) difference = (17 - [hour intValue]) *3600;
if (difference != 0) {
NSTimeInterval interval = 18000 + difference;
dateAlarm = [NSDate dateWithTimeIntervalSinceNow:interval];
}
alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
}
newEvent.startDate = date;
newEvent.endDate = date;
//EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
newEvent.alarms = [NSArray arrayWithObject:alarm];
NSError *error;
BOOL saved = [eventStore saveEvent:newEvent span:EKSpanThisEvent error:&error];
if (!saved && error) {
NSLog(#"%#",[error localizedDescription]);
} else [mo setValue:newEvent.eventIdentifier forKey:#"eventIdentifier"];

iphone how to pass string to another view label?

My problem is how to pass string to another view label? I got try so many example but still can not get the value where I pass.
here is I save the data.
-(IBAction)Save:(id)sender{
timedata = [datePicker date];
NSLog(#"timedata save is = %#",timedata);
time = [NSString stringWithFormat:#"%#",timedata];
NSLog(#"String time = %#",time);
[self dismissModalViewControllerAnimated:YES];
}
here is I want to show the save data.
- (void) viewWillAppear:(BOOL)animated {
show = [[SelectDate alloc]initWithNibName:#"SelectDate" bundle:nil];
show.time = time;
NSLog(#"time = %#",time);
Selectime.text = show.time;
NSLog(#"show.time = %#",show.time);
[super viewWillAppear:animated];
}
If you have set property for time in SelectDate viewController so that it can be accessed in other viewControllers.
//SelectDate.h
NSString *time;
// Your declarations
#property(nonatomic,retain) NSString *time;
//SelectDate.m
#synthesize time;
Now you can use time in other ViewControllers like you are doing.
To get a NSString representation of a date you should look at NSDateFormatter
example:
NSDate* timedata = [datePicker date];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *time = [dateFormat timedata];
[dateFormat release];
Instead of accessing last view, pass your data to your next view:
Hi,My problem is how to pass string to another view label? I got try so many example but still can not get the value where I pass.
here is I save the data.
-(IBAction)Save:(id)sender{
timedata = [datePicker date];
NSLog(#"timedata save is = %#",timedata);
time = [NSString stringWithFormat:#"%#",timedata];
NSLog(#"String time = %#",time);
[self dismissModalViewControllerAnimated:YES];
YourNextView *yourNextView=[[YourNextView alloc] init];
yourNextView.anString=[NSString stringWithFormat:#"%#",timedata];
[yourNextView release];
}
here is You want to show the save data.
- (void) viewWillAppear:(BOOL)animated {
//show = [[SelectDate alloc]initWithNibName:#"SelectDate" bundle:nil];
//show.time = time;
//NSLog(#"time = %#",time);
Selectime.text = self.anString;
NSLog(#"show.time = %#",show.time);
[super viewWillAppear:animated];
}

Memory problem in NSXMLParser (iPhone)

Hi I'm trying to parse an xml and use the currentElementValue inside a code to get an expiredate. This is the code.
if([elementName isEqualToString:#"utlop"]) {
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
int numberOfDays = [currentElementValue intValue];
NSDate *expireDate = [now addTimeInterval:60*60*24*numberOfDays];
NSString *expireString = [dateFormat stringFromDate:expireDate];
NSLog(#"ExpiryString :%#", expireString);
//Add values to Vare
enVare.utlop = expireString;
enVare.enhet = enhet;
enVare.isDirty = NO;
//Add Vare
[appDelegate addVare:enVare];
//Releasing
[dateFormat release];
[enVare release];
enVare = nil;
[currentElementValue release];
currentElementValue = nil;
[expireString release];
expireString = nil;
This results in a memory leak, but Im new to objective C so I can't find the error. When I just do this, it works:
enVare.utlop = currentElementValue;
Do not release objects that are not owned by you. You own an object when u create them using new or alloc. Release only those objects that are created by you using these functions. Also make sure that you release such objects once you have finished using them.