0

I'm struggling with this. I saw some code where you can do this :

- (void)startTimer {
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doActions) userInfo:nil repeats:YES];
} 

Then call it in doActions.

Problem is i want to call it while a button is being pressed, and do actions is an IBaction. I keep getting a sigAbrt.

Can someone give me some sample code where you cay change a label from 'on' to 'off' every 1 second while a button is being pressed?

EDIT

i mean if doActions looks like this

- (IBAction) doActions {
for(int j; j<100; j++){

theLabel.hidden != theLabel.hidden;
//Does invalidate timer go here?
}
}
Glen020
  • 187
  • 1
  • 12
  • 3
    Can you post your `doActions` method? I think it would help us lead you in the right direction - for both design and functional purposes. – Aurum Aquila Feb 20 '11 at 08:55
  • "Then call it in doActions." Wait: are you saying that in `-(IBAction)doActions` you have a line `[self startTimer];`? That would mean that every ~1 second you will create an additional timer for every timer that you've already created. (Which means that after about 20 seconds you'll have crossed a whopping **1 million** timers...) – danyowdee Feb 20 '11 at 16:40
  • Still not sure which of the two following sequences of actions you are talking about? Sequence 1: User touches a button onscreen, User lifts finger again and then some toggle should be switched once every second but no more than 100 times. Sequence 2: User touches screen and _while the finger is still down_ a toggle is switched once every second until the finger is lifted again, but not more than 100 times. Whichever way: A for-loop cannot work. When you start the timer, set an ivar that you increment and check against in your **separate** `timerFired` method. If it's >100 invalidate the timer – danyowdee Feb 21 '11 at 09:50

2 Answers2

1

It still isn't clear to me, what you're trying to accomplish: I would have found it much easier, if you simply had it written down in plain english, after all.

That said, here's what I think will get you where you want to go:

// Assuming ivars:
// NSTimer* toggleTimer
// NSUInteger toggleCount

- (void)toggleTimerFired:(NSTimer*)timer
{
  if ( toggleCount++ >= 100 ) {
    [self stopToggling:self];
    return;
  }
  // do whatever you need to do a hundred times here
}

- (IBAction)stopToggling:(id)sender
{
  [toggleTimer invalidate], toggleTimer = nil;  // you don't want dangling pointers...
  // perform any other needed house-keeping here
}

- (IBAction)startToggling:(id)sender
{
  [self stopToggling:self]; // if `startToggling:` will NEVER be called when a timer exists, this line CAN be omitted.
  toggleCount = 0;
  toggleTimer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(toggleTimerFired:) userInfo:nil repeats:YES];
}

Depending on what exactly you want to do, startToggling: needs to be sent on either touchUpInside or touchDown. In the second case, stopToggling: probably needs to be called on any touchUp... event of the button.

danyowdee
  • 4,658
  • 2
  • 20
  • 35
0
- (void)startTimer {
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doActions) userInfo:nil repeats:YES];
} 

- (void) doActions {
theLabel.hidden != theLabel.hidden;
}
Mats Stijlaart
  • 5,058
  • 7
  • 42
  • 58
  • Thank you, But! wjhat if doactions is (IBaction)doActions. or should i make a seperate function ant then call that within my button action? – Glen020 Feb 20 '11 at 17:43
  • And what if i need it in a loop that loops say 100 times? – Glen020 Feb 20 '11 at 17:45
  • As long as doActions has no parameters @selector(doActions) will do. When it does take parameters, see http://stackoverflow.com/questions/1349740/arguments-in-selector.
    I do not understand what you mean with a loop.. If you mean that the timer needs to be called 100 times? Then increase a counter (instance variable) to achieve this. Add an if statement to invalidate the pauseTimer when a certain amount is reached.
    – Mats Stijlaart Feb 20 '11 at 18:12
  • so... it's nice that you have edited the code, but does it work now? Then close this question, so other people will know the answer is correct. – Mats Stijlaart Feb 22 '11 at 19:34