views:

125

answers:

2

I'm wanting to make one of my methods to run every 60 seconds when my App is running, how would I do that?

+5  A: 

NSTimer

- (void) startTimer
{
  self.myTimer = [NSTimer scheduledTimerWithTimeInterval:60
                                                  target:self
                                                selector:@selector(timerFired:)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void) stopTimer
{
    [self.myTimer invalidate];
}

- (void) timerFired:(NSTimer*)theTimer
{
    NSLog(@"yay");
}
slf
Hmm. http://snapplr.com/fmga
Joshua
You have to add a myTimer property to your class.
Georg
What do you mean?
Joshua
`self.myTimer` indicates that the object has an instance variable called `myTimer`. You have to add a `NSTimer *myTimer` declaration to the list of instance variables for the class.
mipadi
I tried putting `NSTimer *myTimer` into the Header file, but … http://snapplr.com/zyv2
Joshua
-> this should go into your @interface: @property NSTimer *myTimer; This should go into your @implementation: @synthesize myTimer;
Georg
Still one error. http://snapplr.com/r7w7
Joshua
@Joshua - Jeebus! how do you expect to add a property to a class when it's outside of the interface?
Abizern
Joshua, I recommend you re-read http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html to brush up on class definitions
sbooth
Seriously -- go read the basic introduction to cocoa programming guides. These kinds of questions indicate a complete lack of knowledge of the targeted environment. Nothing wrong with that, but fixing it with sequences of questions like the above will get you nowhere fast.
bbum
If his past questions and answers and comments are any indication, Joshua does not read or write code. If you don't provide him a code sample, he's stuck. If you do provide him a code sample, he copies it and pastes it into his app verbatim and then asks in comments about the errors that ensue when it doesn't work.
Peter Hosey
I and others have told him to read the documentation and/or a book before. I think it's pretty clear that he's ignoring that advice and continuing on his current path.
Peter Hosey
And just after I wrote that, I see that he's reasoned his own solution to a problem. Maybe he is making progress after all? http://stackoverflow.com/questions/1483676/making-a-nspopupbutton-display-all-my-ical-calandars#comment-1335225 I'm not saying we should write him off entirely. My suggestion is, don't give him code samples. Give him specific advice in English, perhaps with documentation links, instead.
Peter Hosey
+1  A: 

While the answer is valid, your question is incomplete.

Why do you need to run this method regularly? If it is to poll iCal for tasks every 60 seconds this isn't the best solution. What you need to be doing is observing the notifications that CalCalender store puts outs

Abizern
Really, It needs to be triggered whenever something changes in an NSOutlineView, like a Row is added, deleted or editing. Is there something I could observe for that?
Joshua
You can post your own notifications from the methods that do the adding, deleting and editing.
Abizern
I see. How would you post notifications?
Joshua
This document on notifications should help. It has a section on posting notifications: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html
nall
Even better, use Key-Value Observing. Add yourself as an observer of the array property on the controller that owns it.
Peter Hosey