Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
You can point out an error and give solution

The goal of the forum is not to give out solutions, sorry. If that is what you are looking for, you're looking for it in the wrong places. We're here to help you figure out how to find the solution yourself (either by pointing out appropriate documentation or by giving hints).

or you can tell that person to quit what he's doing because he has no idea. It's a lot easier to say, go read Apples documentation than to point out an error and explain it yourself.

No one told you to quit and pointing out the documentation often times is better than someone trying to explain it. The documentation will be correct, and why type out an explanation to something Apple already documentation (ie, explained) in the proper terms ?

Now if there's something in the documentation you need help clarifying, please feel free to ask questions about the documentation.
 
is that last code enough info balamw?

Still too much left out. "After that I implement a Cancel method pointing to sender (button)" should tell you you are leaving potentially important stuff out.

You can point out an error and give solution or you can tell that person to quit what he's doing because he has no idea. It's a lot easier to say, go read Apples documentation than to point out an error and explain it yourself.

No one is telling you to quit. They're telling you to go back and make sure you understand objects. (Clearly you don't). This is something you will have to understand for yourself.

As it stands you are confusing yourself.

I don't think I've been involved in any of your threads. What resources are you using to learn Objective-C?

B
 
After that I implement a Cancel method pointing to sender (button)

So, my goal is to use 1 start button and 1 cancel button.. and just do their actions. I have set up a the start button to start both timers, obviously both start their countdown at the same time which is not good.

I want to tell one timer to start and if I press cancel, invalidate it. Then If I press start again, call the second timer. (I do this because I read that you can't reuse a timer after you invalidate it).

Some people have suggested to use Booleans like true or false, or conditions. What do you think?

What if after pressing the start button, you create a timer and start it. Then pressing the cancel button invalidates and releases it. Then pressing the start button would create another timer, using the same pointer.

Totally untested and probably broken code below, but should demonstrate the idea:

Code:
-(IBAction)startButton:(id) sender {
	// myTimer is declared in header file ...
	
	if (myTimer!=nil) { // if the pointer already points to a timer, you don't want to create a second one without stoping and destroying the first
			
		[myTimer invalidate];
		[myTimer release];
		
	}
	
	// Now that we know myTimer doesn't point to a timer already..
	
	myTimer = [NSTimer scheduledTimerWithTimeInterval:aTimeInterval target:self 	selector:@selector(echoIt:) userInfo:myDict repeats:YES];
	[myTimer retain];
}

-(IBAction)cancelIt:(id) sender {
	[myTimer invalidate];
	[myTimer release];  // This timer is now gone, and you won't reuse it.
}
 
Thanks for the explanation Knight, I got confuse with pointers and objects.

I'll give a try now. See how it goes.

Man, we could go forever here. hahaa.

wlh99 , you just described exactly what I want to do.
 
Last edited by a moderator:
Update **

It now works !! that logic will help me a lot with future projects.

thanks wlh99 and to everyone who contribute.
 
Nekbeth, you didn't thank Philip Endecott, who posted the solution to your problem on the Apple forum about three hours before wlh99 posted essentially the same solution here.
 
What if after pressing the start button, you create a timer and start it. Then pressing the cancel button invalidates and releases it. Then pressing the start button would create another timer, using the same pointer.

Totally untested and probably broken code below, but should demonstrate the idea:

Code:
-(IBAction)startButton:(id) sender {
	// myTimer is declared in header file ...
	
	if (myTimer!=nil) { // if the pointer already points to a timer, you don't want to create a second one without stoping and destroying the first
			
		[myTimer invalidate];
		[myTimer release];
		
	}
	
	// Now that we know myTimer doesn't point to a timer already..
	
	myTimer = [NSTimer scheduledTimerWithTimeInterval:aTimeInterval target:self 	selector:@selector(echoIt:) userInfo:myDict repeats:YES];
	[myTimer retain];
}

-(IBAction)cancelIt:(id) sender {
	[myTimer invalidate];
	[myTimer release];  // This timer is now gone, and you won't reuse it.
}

Update *** "I though it worked but the timer kept going on the background.

crashed :confused:

wlh99, do you get an exception in the invalid method " [myTimer Invalidate]" ?
 
Nekbeth, you didn't thank Philip Endecott, who posted the solution to your problem on the Apple forum about three hours before wlh99 posted essentially the same solution here.

I did PhoneyDeveloper, it just that his explanation only stops the timer, if I press StartTimer again, the seconds continue where they left. e.g.

startTimer 59,58, cancel.. startTimer 57,56 and so on.



mmm.. I see where there might be problem (my fault, not Phillips).. I'll come back..
 
Last edited:
Update *** "I though it worked but the timer kept going on the background.

crashed :confused:

wlh99, do you get an exception in the invalid method " [myTimer Invalidate]" ?

I didn't test the code at all, so no. But it doesn't surprise me. An exception is thrown when you try to message an object that no longer exists.

I test to see if myTimer is nil as a check to see if the my timer object exists. But elsewhere in the progam I release myTimer and never set myTimer to nil. So, the pointer still points to a memory location, but no object is there so the [myTimer invalidate] fails with an exception. It's a very beginner mistake on my part.

add
mytimer = nil;
to the cancelIt: method.

I strongly recommend reading this document:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

The important thing (assuming you are trying to learn to program) is that you don't just accept that it works, and that instead you know why what you were doing was wrong, and why the answer works.

Look at your first post. Can you say why it crashes? ulbadr's response was pretty direct in his answer, and you didn't understand it. Do you understand it now? Can you say for sure what the code you first posted does, and why it crashes?
 
I see where you going wlh99, and don't worry.. my full intention is to learn, not to get code from all of you. Many people in this thread underestimate my knowledge of objective C (and I understand why, I got lost with the pointers). I have 2 1/2 months since I started development and had 0 idea of the language or programming (I was a Pastry Chef actually :D, which is the name of my first app).

Believe me when I tell you that I know what's going on with my code. I'm aware that If you release an object that it doesn't exist you'll get an exception every time.

Making it work is a lot less important than knowing how to do it, for future work.

Back to the Code, let me go give it try.. b-back


UPDATE**

Ok, it doesn't crash now but timer still won't restart. I'm going to create another timer object (not pointer, I'll use the same pointer). I get this idea that I can't reuse or reset the same timer over again (invalidating and releasing it only pauses the timer). Wish me luck :)
 
Last edited:
I still think it would help us if you described, at a high-level, what it is you are trying to accomplish.

From what I can gather you want a countdown timer: a label that shows the seconds remaining, along with two buttons, one to start the countdown and one to cancel it. After the Start button is tapped, the label will start showing the seconds counting down. If the Cancel button is tapped, the countdown stops and is reset, so that if you tap Start again it begins back at 60 seconds. Is that correct?

If so, I think you need to be aware that a countdown-timer and NSTimer are very different things.
 
Yes, that's exactly what I want to accomplish dejo.

Please, enlighten me .. what is the difference between the countdown-timer and NSTimer?. I though you must use NSTimer to get a countdown or count up timer. Feel free to explain or not, you can also give me link or reference, I'll read it. I want to learn all those stuff.
 
I see where you going wlh99, and don't worry.. my full intention is to learn, not to get code from all of you. Many people in this thread underestimate my knowledge of objective C (and I understand why, I got lost with the pointers). I have 2 1/2 months since I started development and had 0 idea of the language or programming (I was a Pastry Chef actually :D, which is the name of my first app).

Believe me when I tell you that I know what's going on with my code. I'm aware that If you release an object that it doesn't exist you'll get an exception every time.

Making it work is a lot less important than knowing how to do it, for future work.

Back to the Code, let me go give it try.. b-back


UPDATE**

Ok, it doesn't crash now but timer still won't restart. I'm going to create another timer object (not pointer, I'll use the same pointer). I get this idea that I can't reuse or reset the same timer over again (invalidating and releasing it only pauses the timer). Wish me luck :)

Good luck.

Post your echoIt: method. If you are displaying elapsed seconds or something, the code to calculate and display that might be your issue.
 
Thanks, here is the echoIt method :

Code:
- (void) echoIt:(NSTimer *)timer      //  SECONDS METHOD  

{
    
    NSNumber *num = (NSNumber *) [[timer userInfo] valueForKey:@"TotalSeconds"];   //elapsed
    seconds++;

    NSInteger secs = [num integerValue] - seconds;                                 //remaining
    NSLog(@"elapsed: %i, remaining: %i", seconds, secs);
        
    if (secs <= 0)
    {
        [timer invalidate];
        timer = nil;
        
    }
    
    label3.text = [NSString stringWithFormat:@"Seconds remaining: %i", secs];
        
}
 
Yes, that's exactly what I want to accomplish dejo.
Good. Now we're getting somewhere.

Please, enlighten me .. what is the difference between the countdown-timer and NSTimer?
Let me ask you this: what do you think the difference is?

I though you must use NSTimer to get a countdown or count up timer.
Using an NSTimer is certainly a common approach to the problem of modeling a countdown timer, but it's certainly not the only one. Because the timer is tied to the main run loop, it is not guaranteed to actually fire every second (in your case). In that case, perhaps the use of NSDate to keep track of seconds elapsed would be a better approach.
 
Thanks, here is the echoIt method :

Code:
- (void) echoIt:(NSTimer *)timer      //  SECONDS METHOD  

{
    
    NSNumber *num = (NSNumber *) [[timer userInfo] valueForKey:@"TotalSeconds"];   //elapsed
    seconds++;

    NSInteger secs = [num integerValue] - seconds;                                 //remaining
    NSLog(@"elapsed: %i, remaining: %i", seconds, secs);
        
    if (secs <= 0)
    {
        [timer invalidate];
        timer = nil;
        
    }
    
    label3.text = [NSString stringWithFormat:@"Seconds remaining: %i", secs];
        
}

Where is userInfo declared, and as what? Are you passing the same userInfo object to each timer you create, or are you creating a new one? I'm actually not following what you are using that for. I might suggest just passing nil as the user info and ignoring it in this method.
*** Edit: I now see why. For simplicity for now, I would ignore it.

You increment seconds in this method. Where is seconds initalized, and is it re-initalized for the start of the second timer? Or do you just keep incrementing it starting from where it left off? Either could be correct, depending on your goal. A typical handheld stopwatch has a start, stop, and reset, buttons.

As dejo pointed out, the method you are using is a very inaccurate timer. Say you set the interval to 1 sec. (which I assume you did). The timer will fire approxamatly every one second, but not exactly. So by counting seconds as you do the error will add up possibly quickly.

I would second dejo's approach. I would still use an NSTimer, but instead of counting seconds, I would calculate them with NSDate.

The documentation to NSDate is here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
Conviently, it has a method to calculate the time difference between two NSDates.

I hope it's not a rhetorical question, but I really don't know, that is why I asked.

Check out this timer tutorial. I'm trying to follow it by adding a Datepicker to it. Let me know what you think of it.

http://www.youtube.com/watch?v=5jmTQi98vec&feature=related

What do you know about NSTimer? In one sentence, can you tell us precisely what it does? Is that what a your idea of a countdown-timer does?

Do you know what "send a message to a target" means?

I will take a look at the video when I get home tonight.
 
Last edited:
Target is the object that the message is going to execute isn't it. For example, if it's self, that means that those parameters are for the timer object you just created. Please correct me if I'm wrong, I'm not trying to challenge your knowledge, just to learn as I go.

If you see my code before, I'm using NSDate for my timePicker. One favor, I'm not answering more quiz questions, I get your point.. I still need to learn more fundamentals.. I get it, just please contribute with the thread to find solutions or not.. (there are many Professional Forums).
 
Target is the object that the message is going to execute isn't it. For example, if it's self, that means that those parameters are for the timer object you just created. Please correct me if I'm wrong, I'm not trying to challenge your knowledge, just to learn as I go.

If you see my code before, I'm using NSDate for my timePicker. One favor, I'm not answering more quiz questions, I get your point.. I still need to learn more fundamentals.. I get it, just please contribute with the thread to find solutions or not.. (there are many Professional Forums).

If this were a "Professional Forum" I would just give you an answer. I want to know what you do and do not know, so I can help you learn it. So please don't take the questions as condescending, they will help us help you.

Think of objects as people, so to speak. Not only is the NSTimer an object, but so is your viewcontroller. So are the buttons. These objects know how to do things. These things they know how to do are methods. A message is an instruction for an object to do something.

cancelIt: is a method in your viewcontroller object, as are all the methods we have discussed. Then self would refer to the viewcontroller, not the timer. Self would refer to the timer if you had access to apples code that implemets the timer and you were modifiying that.

So a target is the object you are sending a message to. The message is the name of the method you want the object to execute.

[aTimer invalidate]; // tells the timer pointed to by aTimer to execute the invalidate method

When you press a button, a message is sent. The target and method are chosen when you make the connection in Interface Builder. In your case, the target is your viewcontroller, and the method is one of the start or cancel methods.

I asked the question becasue it is fundamental to what an NSTimer is/does.

An NSTimer sends a message to an object at regular intervals.

In your case, the NSTimer is telling your viewcontroller to execute the echoIt: every second. The important part is that your viewcontroller is an object, echoIt: is something your viewcontroller is doing (not the timer). You only have one viewcontroller, so anything it stores (for example seconds) will persit for any NSTimer you create.

Now look at the NSTimer documentation:
Code:
 (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

This returns a pointer to an NSTimer object (NSTimer *).
NSTimeInterval is how often a message will be sent to target.
In your case you use self to refer to your viewcontroller.
selector is the message you will be sending to your viewcontroller.

As a comparision, you could connect a button to echoIt: the same way you connect a button to startButton. If you pressed that button 1 time each second, that is the same thing the timer does.
 
Thanks, that's good information. I actually got caught up trying to finish this function of my App but I plan immediately to deep into books and videos that I already have waiting for me. Believe or not, I'm looking forward to it. I know that as soon as give some time to read over fundamentals like the ones you mention.. it will be easier for and easier for all of you to understand my threads.

So, self refers to my controller.. interesting.
 
Last edited:
One favor, I'm not answering more quiz questions, I get your point.. I still need to learn more fundamentals.. I get it, just please contribute with the thread to find solutions or not.. (there are many Professional Forums).

You clearly have not read the two articles I linked back in post #20 https://forums.macrumors.com/posts/12467980/.

You don't "get it".

The "quiz questions" are necessary because we don't know what it is you know or think you know. We can't read your mind. This is how information is exchanged and we can come to the appropriate level or explanation to be able to help you. It can also help you find the answer yourself by talking through it.

Helping you help yourself is the best way we know how to contribute to the thread.

We've all been there, even the hard-core pros. Sometimes you just can't see the answer that is right in front of your eyes until you try explaining it so someone else.

Please answer this question which I posed earlier in the thread. What books, sites, videos, etc... have you been using to get you to this point and what additional resources are you looking to delve into next.

Given the things it is clear you don't understand, picking the right resources to use to learn the fundamentals you are missing is quite important.

EDIT: Finally, just a comment, PhoneyDeveloper pointed out that you had a parallel thread on the Apple Discussion forums. JMHO, but that's poor netiquette and is a waste of both your time and ours. At least link the two conversations, so folks don't end up repeating what someone else said on the other forum. Even just to say "someone over at the Apple Discussion Fourms (link) suggested ..."

B
 
Last edited:
EDIT: Finally, just a comment, PhoneyDeveloper pointed out that you had a parallel thread on the Apple Discussion forums. JMHO, but that's poor netiquette and is a waste of both your time and ours. At least link the two conversations, so folks don't end up repeating what someone else said on the other forum. Even just to say "someone over at the Apple Discussion Fourms (link) suggested ..."

B

Let me tell you something balamw, and I want you to remember it because it's obvious that you don't get my point either, even if I have said it over 10 times in this thread.

If you don't want to participate on my threads, stay out of it. Nobody is forcing you to read or post comment, alright ? If I want to open 10 threads on the subject in 10 different forums, well.. **** it.. that is how I like it. I'm not wasting anybodies time if they don't want to. Just ignore it and go to another one. I hope this is clear from now on.

About your links.. don't post them again.. I open one and read two paragraphs, I know where thats going. I could post 3 pages explaining you why and how Pro Developers should help Newbies but I not going to do that.

Next thing, quiz question: Just go ahead and ask, you can do as you like here; you'll sometimes get my answers just as sometimes I gets yours.

Again, thanks to all for commenting and helping.

My sources.. well, my main sources is the Apple documentation (all of it), then theres books and all the same stuff than most developers learn from. And.. no I haven't read all of the books, nor watch every video but I will.

I may not understand it all now balamw but give me a year or two and will see who needs some catching up to do.

well, back to work :)
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.