How can I make a Date to jump every n seconds? - objective-c

I want to make the date to change one day every "n" seconds, like a time machine. I have this code but nothing happens, any help will be appreciated.
this is the code: no issues, no error ... no tomorrow date!
-(IBAction)jumpDate
{
NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:#"dd MMMM yyyy h:mm:ss"]; //dateFormater.dateStyle =NSDateFormatterLongStyle; //USA date style MMMM-dd-yyyy
[dateFormater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CDT"]];
NSDate *todayDate = [[NSDate alloc]init];
[gregDate setText : [dateFormater stringFromDate:todayDate]];
[NSString stringWithFormat:#"dd MMMM yyyy", dayCount];
dayCount++;
if (dayCount >= 365)
{
dayCount = 365;
[timerDate invalidate];
}
}
//and the timer
- (void)viewDidLoad
{
NSDateFormatter *dateFormater = [[[NSDateFormatter alloc]init]autorelease];
[dateFormater setDateFormat:#"dd MMMM yyyy"];
NSTimeInterval secondsPerDay = 86400 ; // = 24 * 60 * 60
NSDate *today = [[[NSDate alloc]init]autorelease];
NSDate *tomorrow;
tomorrow = [today dateByAddingTimeInterval:(NSTimeInterval)secondsPerDay];
[gregDate setText : [NSString stringWithFormat:#" %# ",tomorrow]];
dayCount = 1;
timerDate=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(jumpDate)userInfo:nil repeats:YES];
[super viewDidLoad];
}

I have 3 questions, not all directly related to your question.
Why -(IBAction)jumpDate is an IBAction, if you are calling this method from your code you should changed it to (void) and if an IBAction need to call that, call it from an other method that would be the action for that button.
Is there a missing space in your code before userInfo in this call :
timerDate=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(jumpDate)userInfo:nil repeats:YES];
Did you verified that your IBOutlet are all set properly? I've stop counting the times I've forgot that.
[gregDate setText : [NSString stringWithFormat:#" %# ",tomorrow]];
What is gregDate? is it an IBOutlet pointing to a UILabel?
And you've probably verified this, but, that call retunr a valid string?
You should not call your IBAction method from your timer, for 2 reasons,
1. I really think it's bad design, it's introducing confusion on the role of this method since it's also call from a timer.
2. The method signature doesn't match the one NSTimer needs
aSelector:
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
This text come from the NSTimer class reference.

Replace this line:
NSDate *todayDate = [[NSDate alloc]init];
with this line:
NSDate *todayDate = [[NSDate date] dateByAddingTimeInterval:dayCount * 86400];

Related

Working with Date and Time separately using NSDatePickerView on Xcode

I have a part of app that set date and time of an appointment. For this, I have two NSDatePickerView. The first one I set to NSDatePickerModeDate, while the other one to NSDatePickerModeTime. But actually they should be referring to a same NSDate object inside a NSMutableDictionary entry. I know about NSDatePickerModeDateTime, but I need the date and time to be picked separatedly.
I know how to set up the NSDatePickerView to show and hide and event control and such, but at the event control UIControlEventValueChanged fire for NSDatePickerView, I'm confused on how to code the change for this, and also how to initialise the pickers (datePicker.date = today, timePicker.date = "9:00 AM")
#interface MyViewController () {
NSMutableDictionary *data;
}
#end
#implementation MyViewController
#synthesize datePicker, timePicker;
- (void) viewDidLoad {
[super viewDidLoad];
data = [[NSMutableDictionary alloc] init];
[data setObject:[NSDate date] forKey:#"date"];
datePicker.datePickerMode = UIDatePickerModeDate;
timePicker.datePickerMode = UIDatePickerModeTime;
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[timePicker addTarget:self action:#selector(changeTime:) forControlEvents:UIControlEventValueChanged];
datePicker.date = data[#"date"]; //????
timePicker.date = data[#"date"]; //????
}
- (IBAction) changeDate:(id)sender {
UIDatePickerView *dp = (UIDatePickerView *)sender;
[data setObject:dp.date forKey:#"date"]; //????
}
- (IBAction) changeTime:(id)sender {
UIDatePickerView *tp = (UIDatePickerView *)sender;
[data setObject:tp.date forKey:#"date"]; //????
}
The part that I don't know how to code it is denoted by //????. I've read about NSDateFormatter, NSCalendar, and some kind of date components on some answers, but that was actually making me more confused as it also throws strings and structs into the mix, what to use to do what and when. Please help.
You can set both date pickers to the same date and time. The unused part is there but it isn't displayed and can't be changed. When the user changes the value of one date picker you have to set the other date picker to the same value.
- (IBAction)changeDate:(id)sender {
NSDatePicker *dp = (NSDatePicker *)sender;
[data setObject:dp.dateValue forKey:#"date"];
self.timePicker.dateValue = dp.dateValue;
}
- (IBAction)changeTime:(id)sender {
NSDatePicker *tp = (NSDatePicker *)sender;
[data setObject:tp.dateValue forKey:#"date"];
self.datePicker.dateValue = tp.dateValue;
}
u can try this
///Convert Full date to only date
- (IBAction) changeDate:(UIDatePickerView *)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateStr = [dateFormatter stringFromDate:[sender date]];
[data setObject:dateStr forKey:#"date"];
}
///convert Date to only time format
- (IBAction) changeTime:(UIDatePickerView *)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString *dateStr = [dateFormatter stringFromDate:[sender date]];
[data setObject:dateStr forKey:#"Time"];
}

Building a realtime clock in iOS update with NSTimer or performSelector?

I want to display the real time in a label and I found two approaches to update NSDate:
The performSelector approach:
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateTime];
}
- (void)updateTime
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm:ss"];
label.text = [dateFormat stringFromDate:[NSDate date]];
//call updateTime again after 1 second
[self performSelector:#selector(updateTime) withObject:self afterDelay:1.0];
}
The NSTimer approach:
- (void)viewDidLoad
{
[super viewDidLoad];
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime) userInfo:nil repeats:YES];
}
- (void)updateTime
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm:ss"];
label.text = [dateFormat stringFromDate:[NSDate date]];
}
Which one is the better, more performant and reliable method?
Firstly, this is not quite related to Xcode.
Secondly: more performant? Seriously? When it's called one time a second? Just forget worrying about efficiency.
Thirdly, they're equivalent, but I don't see the need for the recursion-like self-invocation of the method. Just go with NSTimer, it's exactly why it was invented.
A timer does its best to perform on a "fixed" schedule, ie every second.
On the other hand, calling performSelector:afterDelay at the end of a function that took, say, 500ms to execute, will result in your function being called every 1500ms (the 500ms it took to execute + the 1s delay).
Thus the timer is more reliable. As H2CO3 said, forget about performance, it's a non-problem with such simple tasks.

How to countdown from a NSDate and display it in hours and minutes

I'm trying to countdown from a NSDate and display it in hours and minutes. Like this: 1h:18min
At the moment my date is updating to a UILabel and counting down but displaying like this:
Here's the code I'm using. A startTimer method and a updateLabel method
- (void)startTimer {
// Set the date you want to count from
// convert date string to date then set to a label
NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
[dateStringParser setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.000Z"];
NSDate *date = [dateStringParser dateFromString:deadlineDate];
NSDateFormatter *labelFormatter = [[NSDateFormatter alloc] init];
[labelFormatter setDateFormat:#"HH-dd-MM-yyyy"];
NSDate *countdownDate = [[NSDate alloc] init];
countdownDate = date;
// Create a timer that fires every second repeatedly and save it in an ivar
NSTimer *timer = [[NSTimer alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateLabel) userInfo:nil repeats:YES];
}
- (void)updateLabel {
// convert date string to date then set to a label
NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
[dateStringParser setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.000Z"];
NSDate *date = [dateStringParser dateFromString:deadlineDate];
NSDateFormatter *labelFormatter = [[NSDateFormatter alloc] init];
[labelFormatter setDateFormat:#"HH-dd-MM"];
NSTimeInterval timeInterval = [date timeIntervalSinceNow]; ///< Assuming this is in the future for now.
self.deadlineLbl.text = [NSString stringWithFormat:#"%f", timeInterval];
}
thanks for any help
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval
{
NSInteger ti = (NSInteger)interval;
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600);
return [NSString stringWithFormat:#"%02i:%02i:%02i", hours, minutes, seconds];
}
Since you use NSTimeInterval ,you are getting the time interval ,ie the difference, in seconds, to display it in hours and minutes you need to apply mathematics logic and convert it!
You many need a few loops to do it.
try this
Regards

ios how to change an image at specific time

I have an image that i want to display as a sun during daytime i.e. from 6am to 6pm, and moon from 6pm to 6am.
I have successfully implemented that but the problem is the image would not change when it reaches the specified time unless re-run the apps before the image change itself.
I don't want to use NSTimer to check the time, like every second. The only possible solution i think of is using NSLocalNotification but I'm a newbiew to it. any help? =)
-(void) dayOrNight
{
NSDate* date = [NSDate date];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"HHmm"];
NSString* dateString = [dateFormat stringFromDate:date];
NSNumber* currentTime = [NSNumber numberWithInt:[dateString intValue]];
NSNumber* daytime = [NSNumber numberWithInt:600];
NSNumber* nightime = [NSNumber numberWithInt:1800];
NSLog(#"current time: %#",dateString);
dayNight = [[UIImageView alloc] initWithFrame:CGRectMake(265, 10, 50, 50)];
if ( [currentTime doubleValue] >= [daytime doubleValue] && [currentTime doubleValue] <= [nightime doubleValue] )
{
dayNight.image = [UIImage imageNamed:#"Sun.png"];
}
else
{
dayNight.image = [UIImage imageNamed:#"moon.png"];
}
[self.view addSubview:dayNight];
}
Local notification should be fine, I guess.
Here you can get all needed code snippets to implement execution of dayOrNight method at needed time. Also, you shouldn't add new view every time you change the picture.
I think it is impossible to do without NSTimer. U can set refreshTime by yourself (for example 1 min/ if u don`t want to do it every second)))
Or u can call this method in other methods, which are working in your class every time...
maybe u have some object, which u use during your class is working...
-(IBAction)myButtonWhichIPressDuringIworkHere {
///some actions
[self dayOrNight];
}
In other case you should to use NSTimer
you don't need to check the time at every second.
this is one of the possible solutions. it has been tested on real device only.
your UIYourViewController.m
- (void)viewWillAppear:(BOOL)animated {
NSDate *_currentDate = [NSDate date];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:#"yyyy-MM-dd' 06:00AM +0000'"]; // set the morning date here
NSString *_morningDateString = [_dateFormatter stringFromDate:_currentDate];
[_dateFormatter setDateFormat:#"yyyy-MM-dd' 06:00PM +0000'"]; // set the evening date here
NSString *_eveningDateString = [_dateFormatter stringFromDate:_currentDate];
[_dateFormatter setDateFormat:#"yyyy-MM-dd hh:mma zzz"];
NSDate *_morningDate = [_dateFormatter dateFromString:_morningDateString];
NSDate *_eveningDate = [_dateFormatter dateFromString:_eveningDateString];
NSTimeInterval _intervalToMorningDate = [_morningDate timeIntervalSinceDate:_currentDate];
NSTimeInterval _intervalToEveningDate = [_eveningDate timeIntervalSinceDate:_currentDate];
if (_intervalToMorningDate > 0) {
// now it is night
dayNight.image = [UIImage imageNamed:#"moon.png"];
[self performSelector:#selector(replaceTheBackgoundForMorning) withObject:nil afterDelay:_intervalToMorningDate];
} else {
// now it is daytime
dayNight.image = [UIImage imageNamed:#"Sun.png"];
[self performSelector:#selector(replaceTheBackgoundForEvening) withObject:nil afterDelay:_intervalToEveningDate];
}
}
- (void)viewDidDisappear:(BOOL)animated {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
and finally you should add them to your same .m file:
-(void)replaceTheBackgoundForMorning {
// reaplce the backgound here
dayNight.image = [UIImage imageNamed:#"Sun.png"];
}
- (void)replaceTheBackgoundForEvening {
// reaplce the backgoung here
dayNight.image = [UIImage imageNamed:#"moon.png"];
}

Using NSDate in While loop

I want to get the current date using [NSDate date] in a While Loop. I accomplish this by doing like this:
while (interval > 0.0) {
NSDate *currentDate = [[NSDate alloc] init];
currentDate = [NSDate date];
interval = (float) [newDate timeIntervalSinceDate: currentDate] / 60;
[currentDate release];
}
I dont know why is the Memory leaks shows that there is a great amount of memory is leaked. Kindly guide me that what is the right way to accomplish my task.
In line NSDate *currentDate = [[NSDate alloc] init]; you create a new object, which you should release. In line currentDate = [NSDate date]; you do not release an old object, you only make a pointer to point to another object. In line [currentDate release]; you release an object created on the second line of a loop, which may cause an error (that object is marked as autorelease one and iOS will clean it for you). You should rewrite your code like:
while (interval > 0.0) {
NSDate *currentDate = [NSDate date];
interval = (float) [newDate timeIntervalSinceDate: currentDate] / 60;
}
You don't need the first line NSDate *currentDate = [[NSDate alloc] init];. You can directly assign the [NSDate date] to currentDate.
NSDate *currentDate = nil;
while (interval > 0.0) {
currentDate = [NSDate date];
interval = (float) [newDate timeIntervalSinceDate: currentDate] / 60;
}
The problem is not that you are leaking per se but that you are running in a while loop.
The auto released dates are growing in the autorelease pool because the pool only empties in the idle time on the run loop.
One solution is to create a local autorelease pool within the scope of the while
while (foo) {
NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc ] init];
NSDate *currentDate = [NSDate date];
// other computational foo
[aPool release]
}
When you release the pool in the local scope it will immediately drop the autoreleased date you requested.