Adding two values in the strings together in ios - iphone

I would like to add the values in the strings together which are in the format:"HH:mm:ss.SSS" and display it back again. In my case, it is adding timeString with difference to get currentString. I am not sure of the correct way of doing it. I am definitely doing it wrongly. The problem i face is that when i click the stop button and click start again, it starts off from 0 again.. I would like it to start off from the same spot where it left off..
The portion of the code is here:
-(void)updateTimer{
currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
timeString=[dateFormatter stringFromDate:timerDate];
currentString=[dateFormatter stringFromDate:timerDate];
//double doubleOfString = [timeString doubleValue] + [difference doubleValue];
//currentString = [NSString stringWithFormat:#"%f",doubleOfString];
lbl.text = currentString;
}
The entire code is here:
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
UILabel *lbl;
NSTimer *stopTimer;
NSDate *startDate,*currentDate;
BOOL running,lap;
UIButton *bttn;
NSMutableArray *tableItems;
NSString *timeString,*currentString,*difference;
UITableView *tableview;
}
#property (strong,nonatomic) IBOutlet UILabel *lbl;
#property (strong,nonatomic) IBOutlet UIButton *bttn;
#property (strong,nonatomic) NSMutableArray *tableItems;
#property (strong,nonatomic) NSString *timeString;
#property (strong,nonatomic) IBOutlet UITableView *tableview;
-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;
-(void)updateTimer;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize lbl,bttn,tableItems,timeString,tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
lbl.text = #"00.00.00.000";
running = FALSE;
lap = FALSE;
difference = #"0";
startDate = [NSDate date];
tableItems = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
if(!running){
running = TRUE;
lap = TRUE;
[sender setTitle:#"Stop" forState:UIControlStateNormal];
[bttn setTitle:#"Lap" forState:UIControlStateNormal];
if (stopTimer == nil) {
stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
}
}else{
running = FALSE;
lap = FALSE;
startDate = currentDate;
difference = currentString;
[sender setTitle:#"Start" forState:UIControlStateNormal];
[bttn setTitle:#"Restart" forState:UIControlStateNormal];
[stopTimer invalidate];
stopTimer = nil;
}
}
-(void)updateTimer{
currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
timeString=[dateFormatter stringFromDate:timerDate];
currentString=[dateFormatter stringFromDate:timerDate];
double doubleOfString = [timeString doubleValue] + [difference doubleValue];
//currentString = [NSString stringWithFormat:#"%f",doubleOfString];
lbl.text = currentString;
}
-(IBAction)resetPressed:(id)sender{
if (!lap) {
[stopTimer invalidate];
stopTimer = nil;
tableItems = [[NSMutableArray alloc] init];
startDate = [NSDate date];
lbl.text = #"00.00.00.000";
running = FALSE;
}
else{
[tableItems insertObject:timeString atIndex:0];
[tableview reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Step 1:Check whether if we can reuse a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
//Step2: If there are no new cells to reuse,create a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:#"cell"];
//UIView *v = [[UIView alloc] init];
//v.backgroundColor = [UIColor redColor];
//cell.selectedBackgroundView = v;
//changing the radius of the corners
//cell.layer.cornerRadius = 10;
}
//Step 3: Set the cell text content
cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
//Step 4: Return the row
return cell;
}
#end
I welcome any other corrections that you can suggest...

I would kindly suggest not applying math to strings.
I mean:
What's the sum of the words “apple” and “Millenium Falcon”? Doesn’t make any sense, does it?
So why are you trying to form an addition of two strings, when you had a perfectly good number (timeInterval) — a type perfectly suitable to perform algebraic operations on?
PS:
NSDateFormatter instances want to be reused. Stash yours in an instance variable.

Your update timer function will be:Firstly add int counter in .h file . In viewDidLoad counter = 0;
-(void)updateTimer{
counter ++;
int hours = counter / 3600 ;
int minutes = (counter / 60) - (hours * 60);
int seconds = counter - (hours * 3600) - (minutes * 60);
lbl.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
}

Related

DatePicker is not returning the time/date, which i had specified when I had created it is in edit view

IPhone: I am doing my remainder app in Iphone,While i navigate from my "create new"-view to "edit"-View, I need my DatePicker to display the Value i have stored already when i just created the remainder, HENCE I need the DatePicker to return the time/date i specified when it is in edit view -Pls Help
1.My RemainderSetUpViewcontroller.h
`
#protocol RemainderDelegate <NSObject>
-(void)store:(NSArray *) remainderDel:(NSArray *) TimeDateDel;
#end
#interface RemainderSetUpViewcontroller : UIViewController<UITextFieldDelegate,UITextFieldDelegate>
{
UITextField *textField1;
NSString *str;
UIDatePicker *datePicker;
NSDate *date;
NSMutableArray *remainder;
NSMutableArray *TimeDate;
UILocalNotification* notification;
}
#property (strong, nonatomic)IBOutlet NSString* remainderr;
#property (strong, nonatomic)IBOutlet NSString *index;
#property(strong, nonatomic) id <RemainderDelegate> delegate;
-(void)addRemainder;
-(void)actionDone;
#end
`
2My RemainderSetUpViewcontroller.m:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem *add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(addRemainder)];
self.navigationItem.rightBarButtonItem=add;
//TextField1...............................................................................................................
self.title=#"Add Remainder";
textField1 = [[UITextField alloc]initWithFrame:CGRectMake(20, 60, 280, 34)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField1.delegate = self;
textField1.placeholder = #"Enter the Reminder name";
[self.view addSubview:textField1];
//datePicker&TextField2....................................................................................................
datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0,210, self.view.frame.size.width, self.view.frame.size.height)];
[datePicker addTarget:self action:#selector(actionDone) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
//UserDefaults.............................................................................................................
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
remainder = [[NSMutableArray alloc]init];
TimeDate = [[NSMutableArray alloc]init];
if ([ud objectForKey:#"remainder"])
{
remainder = [[ud objectForKey:#"remainder"]mutableCopy];
TimeDate = [[ud objectForKey:#"TimeDate"]mutableCopy];
}
//to make the fields visible when we try to edit it............................................................................
if ([remainderr isEqualToString:#"edit"])
{
self.title = #"Reminder Edit";
textField1.text = [remainder objectAtIndex:[index integerValue]];
str = [TimeDate objectAtIndex:[index integerValue]];
}
}
#pragma mark - doneClicked
//DoneButton.................................................................................................................
-(void)actionDone
{
date = datePicker.date;
NSDateFormatter *dateform=[[NSDateFormatter alloc]init];
[datePicker setDate:[NSDate date]];
dateform.dateFormat = #"dd-MM-YYYY HH:mm:SS";
str=[dateform stringFromDate:date];
}
#pragma mark - AddRemainder Notifications
//addButton.................................................................................................................
-(void)addRemainder
{
//LocalNotification.........................................................................................................
notification = [[UILocalNotification alloc] init];
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([remainderr isEqualToString:#"edit"])
{
[remainder replaceObjectAtIndex:[index integerValue] withObject:textField1.text];
[TimeDate replaceObjectAtIndex:[index integerValue] withObject: str];
[self.navigationController popViewControllerAnimated:YES];
}
else if([remainderr isEqualToString:#"delete"])
{
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
else
{
if((textField1.text!=NULL)&&( date!=NULL))
{
[remainder addObject:textField1.text];
[TimeDate addObject: str];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:#"Alert !" message:#"Please Give a title for your Remainder " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[myAlert show];
}
}
//Adding objects............................................................................................................
//NSLog(#"%# %#", remainder, TimeDate);
[self.delegate store:remainder :TimeDate];
NSLog(#"The notifications is \n %#",notification);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end
`
Thank you.
To display saved date in DatePicker, You need to set saved date in datepicker.
[datePicker setDate:savedDate animated:NO];
To display saved date in DatePicker, You need to set saved date in datepicker.
[datePicker setDate:savedDate animated:NO];
to get Date from datePicker
NSDate *date = datePicker.date;

UIDatePicker not returning my current time and date in default

In default While I create a UIDate picker in my remainder app, when i click done, the current time has to be entered in default or if the date picker is animated the date value can change but in my case the current time is not returned in default ! Please help, 1.My RemainderSetUpViewcontroller.h
`
#protocol RemainderDelegate <NSObject>
-(void)store:(NSArray *) remainderDel:(NSArray *) TimeDateDel;
#end
#interface RemainderSetUpViewcontroller : UIViewController<UITextFieldDelegate,UITextFieldDelegate>
{
UITextField *textField1;
NSString *str;
UIDatePicker *datePicker;
NSDate *date;
NSMutableArray *remainder;
NSMutableArray *TimeDate;
UILocalNotification* notification;
}
#property (strong, nonatomic)IBOutlet NSString* remainderr;
#property (strong, nonatomic)IBOutlet NSString *index;
#property(strong, nonatomic) id <RemainderDelegate> delegate;
-(void)addRemainder;
-(void)actionDone;
#end
`
2My RemainderSetUpViewcontroller.m:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem *add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(addRemainder)];
self.navigationItem.rightBarButtonItem=add;
//TextField1...............................................................................................................
self.title=#"Add Remainder";
textField1 = [[UITextField alloc]initWithFrame:CGRectMake(20, 60, 280, 34)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField1.delegate = self;
textField1.placeholder = #"Enter the Reminder name";
[self.view addSubview:textField1];
//datePicker&TextField2....................................................................................................
datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0,210, self.view.frame.size.width, self.view.frame.size.height)];
[datePicker addTarget:self action:#selector(actionDone) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
//UserDefaults.............................................................................................................
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
remainder = [[NSMutableArray alloc]init];
TimeDate = [[NSMutableArray alloc]init];
if ([ud objectForKey:#"remainder"])
{
remainder = [[ud objectForKey:#"remainder"]mutableCopy];
TimeDate = [[ud objectForKey:#"TimeDate"]mutableCopy];
}
//to make the fields visible when we try to edit it............................................................................
if ([remainderr isEqualToString:#"edit"])
{
self.title = #"Reminder Edit";
textField1.text = [remainder objectAtIndex:[index integerValue]];
str = [TimeDate objectAtIndex:[index integerValue]];
}
}
#pragma mark - doneClicked
//DoneButton.................................................................................................................
-(void)actionDone
{
date = datePicker.date;
NSDateFormatter *dateform=[[NSDateFormatter alloc]init];
[datePicker setDate:[NSDate date]];
dateform.dateFormat = #"dd-MM-YYYY HH:mm:SS";
str=[dateform stringFromDate:date];
}
#pragma mark - AddRemainder Notifications
//addButton.................................................................................................................
-(void)addRemainder
{
//LocalNotification.........................................................................................................
notification = [[UILocalNotification alloc] init];
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([remainderr isEqualToString:#"edit"])
{
[remainder replaceObjectAtIndex:[index integerValue] withObject:textField1.text];
[TimeDate replaceObjectAtIndex:[index integerValue] withObject: str];
[self.navigationController popViewControllerAnimated:YES];
}
else if([remainderr isEqualToString:#"delete"])
{
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
else
{
if((textField1.text!=NULL)&&( date!=NULL))
{
[remainder addObject:textField1.text];
[TimeDate addObject: str];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:#"Alert !" message:#"Please Give a title for your Remainder " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[myAlert show];
}
}
//Adding objects............................................................................................................
//NSLog(#"%# %#", remainder, TimeDate);
[self.delegate store:remainder :TimeDate];
NSLog(#"The notifications is \n %#",notification);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end
`
Thank you.
Already a descusion carried about this please check the link. Delete a particular local notification
Just add one line after a line in your code
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
Add Below line to your code and try
datePicker.date=[NSDate date];

Retrieve EKEventStore

I have a table with all the users events in my app. When you click on one of the events I would like it to save the event name, location, start date, end date, notes in a UILabel. How can I do this my table code is bellow. I have tried searching the web for hours and nothing comes up.
Below is the code for the table
//eventsTable.h
#import <UIKit/UIKit.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
#interface eventsTable : UIViewController <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate> {
IBOutlet UITableView *Table;
EKEventStore *eventStore;
EKEvent *event;
EKEventViewController *detailViewController;
EKCalendar *defaultCalendar;
NSMutableArray *eventsList;
}
-(IBAction) done;
- (NSArray *)fetchEventsForToday;
#property (nonatomic, retain)
IBOutlet UITableView *Table;
#property (nonatomic, retain) EKEventStore *eventStore;
#property (nonatomic, retain) EKCalendar *defaultCalendar;
#property (nonatomic, retain) NSMutableArray *eventsList;
#property (nonatomic, retain) EKEventViewController *detailViewController;
#end
//eventsTable.m
#import "eventsTable.h"
#implementation eventsTable
#synthesize eventsList, eventStore, defaultCalendar, detailViewController,Table;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
// Create an Add button
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[Table reloadData];
}
-(IBAction) done{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table view data source
// Fetching events happening in the next 24 hours with a predicate, limiting to the default calendar
- (NSArray *)fetchEventsForToday {
NSDate *startDate1 = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate1 = [NSDate distantFuture];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
//NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
// calendars:calendarArray];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate1 endDate:endDate1 calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
events =
[events sortedArrayUsingSelector:
#selector(compareStartDateWithEvent:)];
self.eventsList = [NSMutableArray arrayWithArray:events];
[Table reloadData];
return events;
}
#pragma mark -
#pragma mark Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return eventsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:self.Table] == YES){
static NSString *EventsCellIdentifier = #"Events";
/* We have the index path so let's get the corresponding
event from the array of events */
EKEvent *event1 = [self.eventsList
objectAtIndex:indexPath.row];
/* Try to get a reusable table cell */
result =
[tableView dequeueReusableCellWithIdentifier:EventsCellIdentifier];
if (result == nil){
result = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:EventsCellIdentifier] autorelease];
}
/* The title text of the cell will be the title of the event */
result.textLabel.text = event1.title;
result.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
result.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
/* Now let's format the date and the time of the event
and display it as the subtitle of the cell */
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.startDate];
NSDateComponents *components1 =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.endDate];
if ([components hour] == 0 &&
[components minute] == 0 &&
[components second] == 0){
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"All Day from %# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components1 month],
(long)[components1 day],
(long)[components1 year]];
} else {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"%# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components hour],
(long)[components minute],
(long)[components1 hour],
(long)[components1 minute]];
}
}
return(result);
[event release];
[eventsList release];
[eventStore release];
[result release];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)viewDidUnload {
self.eventsList = nil;
self.view = nil;
self.Table = nil;
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
[Table release];
[eventStore release];
[eventsList release];
[defaultCalendar release];
[detailViewController release];
}
#end
Remove the [Table reloadData] and [self.eventsList addObjectsFromArray:[self fetchEventsForToday]]; from viewDidload and place it in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
return [self.eventsList count];
}
Hope this will help you. but it will be better to debug the method [self fetchEventsForToday].

UITableView with sections

I have an app and in one view is a table. In the table I call the EventStore So It fills the table with your calendar events. I would like to have bars in the table like the ical app on the iphone so It breaks the days up with A bar the bar has the date (ex. March 25, 2011). Below is the code for the table
//eventsTable.h
#import <UIKit/UIKit.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
#interface eventsTable : UIViewController <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate> {
IBOutlet UITableView *Table;
EKEventStore *eventStore;
EKEvent *event;
EKEventViewController *detailViewController;
EKCalendar *defaultCalendar;
NSMutableArray *eventsList;
}
-(IBAction) done;
- (NSArray *)fetchEventsForToday;
#property (nonatomic, retain)
IBOutlet UITableView *Table;
#property (nonatomic, retain) EKEventStore *eventStore;
#property (nonatomic, retain) EKCalendar *defaultCalendar;
#property (nonatomic, retain) NSMutableArray *eventsList;
#property (nonatomic, retain) EKEventViewController *detailViewController;
#end
//eventsTable.m
#import "eventsTable.h"
#implementation eventsTable
#synthesize eventsList, eventStore, defaultCalendar, detailViewController,Table;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
// Create an Add button
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[Table reloadData];
}
-(IBAction) done{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table view data source
// Fetching events happening in the next 24 hours with a predicate, limiting to the default calendar
- (NSArray *)fetchEventsForToday {
NSDate *startDate1 = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate1 = [NSDate distantFuture];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
//NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
// calendars:calendarArray];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate1 endDate:endDate1 calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
events =
[events sortedArrayUsingSelector:
#selector(compareStartDateWithEvent:)];
self.eventsList = [NSMutableArray arrayWithArray:events];
[Table reloadData];
return events;
}
#pragma mark -
#pragma mark Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return eventsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:self.Table] == YES){
static NSString *EventsCellIdentifier = #"Events";
/* We have the index path so let's get the corresponding
event from the array of events */
EKEvent *event1 = [self.eventsList
objectAtIndex:indexPath.row];
/* Try to get a reusable table cell */
result =
[tableView dequeueReusableCellWithIdentifier:EventsCellIdentifier];
if (result == nil){
result = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:EventsCellIdentifier] autorelease];
}
/* The title text of the cell will be the title of the event */
result.textLabel.text = event1.title;
result.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
result.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
/* Now let's format the date and the time of the event
and display it as the subtitle of the cell */
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.startDate];
NSDateComponents *components1 =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.endDate];
if ([components hour] == 0 &&
[components minute] == 0 &&
[components second] == 0){
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"All Day from %# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components1 month],
(long)[components1 day],
(long)[components1 year]];
} else {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"%# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components hour],
(long)[components minute],
(long)[components1 hour],
(long)[components1 minute]];
}
}
return(result);
[event release];
[eventsList release];
[eventStore release];
[result release];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)viewDidUnload {
self.eventsList = nil;
self.view = nil;
self.Table = nil;
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
[Table release];
[eventStore release];
[eventsList release];
[defaultCalendar release];
[detailViewController release];
}
#end
You are going to have to split each day up into a separate section and implement the proper UITableViewDataSource protocols for section indexes: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
Here is a tutorial: http://www.iphonedevcentral.com/indexed-uitableview-tutorial/
If I understand your question correctly, you need to change the section titles with the date you want to display. You do this by implementing the (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section method.
You have a problem in your code despite the question. You must release the resources before the return.
return(result);
[event release];
[eventsList release];
[eventStore release];
[result release];

NSDate basic usage error

Next to a label showing the value of a slider i want to show the date that corresponds to a start date + the number of days in slider value units.
When i move the slider the app crashes in the Simulator. As far as i could find out that happens when i try to create a new NSDate field and my startDate object being "out of scope".
Reading a lot regarding retaining and releasing NSDate documentation had not helped me until now, because the documentation is so abstract that it seems to me that I'm not able to find the relevant part(s) in the documentation. I hope to understand more by example.
So what's wrong with the following code (and from where in the documentation should a beginner have learned that) ?
#import <UIKit/UIKit.h>
#interface SliderDateViewController : UIViewController {
UILabel *dateLabel;
UILabel *sliderValueLabel;
UISlider *slider;
NSDate *startDate;
}
#property (nonatomic, retain) IBOutlet UILabel *dateLabel;
#property (nonatomic, retain) IBOutlet UILabel *sliderValueLabel;
#property (nonatomic, retain) IBOutlet UISlider *slider;
#property (nonatomic, retain) NSDate *startDate;
-(IBAction)sliderValueChanged:(UISlider *)theSlider;
#end
#import "SliderDateViewController.h"
#implementation SliderDateViewController
#synthesize dateLabel;
#synthesize sliderValueLabel;
#synthesize slider;
#synthesize startDate;
- (void)viewDidLoad {
[super viewDidLoad];
dateLabel.text = #"01.01.2010";
sliderValueLabel.text = #"0";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd.MM.yyyy"];
self.startDate = [inputFormatter dateFromString:dateLabel.text];
NSLog(#"startDate: ", self.startDate.description);
[inputFormatter release];
}
-(IBAction)sliderValueChanged:(UISlider *)theSlider {
int sliderValueAsInt = (int)(theSlider.value + 0.5f);
NSString *newText = [[NSString alloc] initWithFormat:#"%d", sliderValueAsInt];
sliderValueLabel.text = newText;
[newText release];
// Next line following the comment crashed with
// *** -[CFDate release]: message sent to deallocated instance 0x3b07d10
NSDate *newDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"dd.MM.yyyy"];
dateLabel.text = [outputFormatter stringFromDate:newDate];
[outputFormatter release];
// [newDate release] // <- this was the error
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[dateLabel release];
[sliderValueLabel release];
[slider release];
[startDate release];
[super dealloc];
}
#end
Firstly, -addTimeInterval: is deprecated. Use -dateByAddingTimeInterval: instead.
NSDate *newDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
[startDate release];
startDate = newDate;
[newDate release];
Since -addTimeInterval: is not an +alloc/-copy/-create method, the newDate is owned by no one and you should not -release it. To obtain the ownership you need to
NSDate *newDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
if (newDate != startDate) {
[startDate release];
startDate = [newDate retain];
}
which can be conveniently rewritten as
self.startDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
Judging from the code logic, the startDate should be kept immutable. That means the startDate = newDate; stuff shouldn't appear at all. Delete the 3 lines below NSDate *newDate = [startDate ...]; and the code shall work fine.