Here the nil gets passed in to your cancelIt: method as the timer parameter. This is always going the be nil, which explains why you can't cancel somethign that doesnt exist.
So guys, could please give a hand and write the code as it should be. Should I declare it as an instance variable of NSTimer ? The reason I'm declaring it in a instance method is because I had another similar method before.
It would be helpful if you create an instance variable and store this created timer in it. That way, when you go to cancel it, you have a pointer to it.
It would be helpful if you create an instance variable and store this created timer in it. That way, when you go to cancel it, you have a pointer to it.
OOh I see what missing now, your right.. I had not Timer, only a pointer.
I'll give a try. Thanks
By the way, do you know how to make the seconds stop at 60 and loop instead of showing 566 seconds remaining?
What do you mean like when it reached 60 it goes back down to 0 again and starts its way up back to 60? If so just say something like
Code:
if (seconds > 60) {
seconds = seconds - 60;
}
Thanks Domele, it's the other way around, when it reaches 0 it goes back to 60. About the other thing, I was able to call the timer method, here is the code:
The alert is just a test, It works fine when I press the button, but I don't think I'm accessing the timer's info because the "timer invalidate" is not responding. So my guess is that I need to add something inside the "userInfo parameter" in the declaration, what do you think?
Thanks Domele, it's the other way around, when it reaches 0 it goes back to 60. About the other thing, I was able to call the timer method, here is the code:
The alert is just a test, It works fine when I press the button, but I don't think I'm accessing the timer's info because the "timer invalidate" is not responding. So my guess is that I need to add something inside the "userInfo parameter" in the declaration, what do you think?
And in your cancelIt method, use your theTimer instance variable to cancel the timer:
Code:
- (void) cancelTimer; // Do not include a time in this method's parameters
{
[theTimer invalidate]; //This is the timer object we saved in "cancelTime", above
theTimer = nil; //Set "theTimer" to nil since it's no longer valid.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"An Alert!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Great, thank you Duncan. I really needed that variable. Only thing is that I did not create a new timer, i used the old one with the new variable, then I used it like you pointed out. Cancel button, finally worked .
* Should I call a refresh or reload method on the timer after I cancel ?
Great, thank you Duncan. I really needed that variable. Only thing is that I did not create a new timer, i used the old one with the new variable, then I used it like you pointed out. Cancel button, finally worked .
* Should I call a refresh or reload method on the timer after I cancel ?
You can't reuse timers. After you invalidate them you have to create a new one.
I generally don't retain timers, since when you schedule them, the system retains them. I save them in a non-retained property. I'll sometimes set up a custom setter that invalidates the old timer, like this:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You can't reuse timers. After you invalidate them you have to create a new one.
I generally don't retain timers, since when you schedule them, the system retains them. I save them in a non-retained property. I'll sometimes set up a custom setter that invalidates the old timer, like this:
then in .m , I implement the methods, here is the method for new timer:
Code:
---------------- NEW TIMER -----------------
- (IBAction) newActionTimer: (id)sender
{
newTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(echoNewActionTimer:) userInfo:nil repeats:NO];
}
- (void) echoNewActionTimer: (NSTimer *)timer2
{
[myTimer invalidate]; // Timer seems to work because it pauses the logged timed elapsed
myTimer = nil;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Invalidate"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
// is my newTimer On right now ??
}
The result is that the original Timer stops or invalidates but I still don't know if my new Timer is On. The new Timer also needs to be filled with userInfo, I'll do that after I know it's being called.
Please, if you see any fundamental errors, let me know..