views:

66

answers:

4

[Cocoa/Objective-C] I adapted a timer routine (for current time) from this site (and it works great - thanks). It's currently attached to a button. My question is: How do I make it start when my app starts up (and not use a button) (in other languages, I'd simply put the action listener or timer in the Form)...?

Thank for any help on this!

+7  A: 

In your application delegate you'll find a method called

- (void)applicationDidFinishLaunching:(UIApplication *)application

I guess that would be where to start a timer on app startup.

tmadsen
A: 

Thank you. I see the lines: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application

}

I put my code inside but, one of the elements of my code is a textfield and I get an error saying the textfield does not exist. How do I correct this? What do I do about the "aNotification" (I don't know what it means)?

Mr T
+2  A: 

Put it in your awakeFromNib method. This is called on all objects that get deserialized from your nib (like your application delegate), but it isn't called until all objects are deserialized and wired (so you can use your text field, for instance). For example:

- (void)timerFired:(NSTimer*)timer
{
  NSLog(@"Timer completed!");
}

- (void)awakeFromNib
{
  [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
}

Obviously, in this simple example the timer could have been created in either the applicationDidFinishLaunching: method or the awakeFromNib method since it doesn't interact with any other serialized objects, but in your case, it sounds like you need the awakeFromNib method.

Jason Coco
A: 

Perfect! Awake from nib did it! Thanks!!

Mr T