I have a lot of search about this and found many solution but no one works in my case.
i am using a NSTimer and activate it from a button click here its work fine . Now i invalidate timer on second button click and start it again on third button click but on third button click my timer not works. Can anyone tell me what is the wrong with me.
Code which i am using.
button1 click :
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateCountdown) userInfo:nil repeats:YES];
button2 click :
if (timer != nil && [timer isValid])
{
[timer invalidate];
timer=nil;
}
button3 click :
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateCountdown) userInfo:nil repeats:YES];
now on click on button3 timer not working.
by looking at your code . It must work . do couple of trials:
Check the your 3 rd button click event is being called or not .
Check if you are doing timer=nil , [timer invalidate]; somewhere else .
Related
Here I write the code to move the imageView on click. I want to perform move up on first click and if it is clicked again it goes back. Here's my code :
-(void)gdown
{
if (penview.center.y > 428) penview.center = CGPointMake(penview.center.x, penview.center.y -5);
if(penview.center.x ==428)
{
[movtimer invalidate];
}
}
-(void)buttonmover
{
movtimer=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(gdown) userInfo:nil repeats:YES];
if(movtimer==nil)
{
movtimer=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(gdown) userInfo:nil repeats:YES];
}
}
-(void)gdup
{
if (penview.center.y < 480) penview.center = CGPointMake(penview.center.x, penview.center.y +5);
if(penview.center.y ==480)
{
[movtimers invalidate];
}
}
-(void)buttonmovup
{
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(gdup) userInfo:nil repeats:YES];
if(movtimers==nil)
{
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(gdup) userInfo:nil repeats:YES];
}
}
The method performing the click operations :
-(IBAction)popupview:(id)sender
{
UIButton *button = sender;
if(button.selected) //
{
[self buttonmover];//gooin up
button.selected = false;
}
else
{
[self buttonmovup];//button goin down
button.selected = true;
}
}
Its not working. The image view shakes while clicking. What change should I make in this method?
I don't think your else part code is getting executed. You should maintain a flag in your class for the same as selection property will always be true when button is tapped.
Your code is right.Only do following change
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(gdown) userInfo:nil repeats:NO];
for every timer set repeat to NO instead of YES
Your code is right.Only do following change
movtimers=[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(gdown) userInfo:nil repeats:NO];
for every timer set repeat to NO instead of YES
Ok if you want to show animation then change your if statement like this
//in gdown
if(penview.center.y <=428)
{
[movtimers invalidate];
}
//in gup
if(penview.center.y >=480)
{
[movtimers invalidate];
}
I have three tabbar items in one View. There is one NSTimer Schedule on Start Button Action in first tab item.When i press another tab item and come to the recent tab item(i.e tab item with NSTimer), it increases the seconds with 2. If i do the same for second time it increases the second with 3 and so on.I want seconds to be increased by 1.
This is the code i am using to schedule NSTimer
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
I have tried to solve it using [timer invalidate]; but it is not giving me a proper result.
Thanks in advance.
in the - (void)viewWillAppear:(BOOL)animated
method make timer invalidate by [timer invalidate];
and again start it by :
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
I have a timer which i am using in my application..
In my application ,I have two views. On the first view i have added a button and a progress bar and i have used a timer for progress bar.
User can move to next view either on clicking the button or wait for progress bar to complete..
If the progress bar is completed the user will be drawn to next view..
Here, is my question How to stop timer which have no name like...
[NSTimer scheduledTimerWithTimeInterval:0.0005 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
Because of not stopping the timer in the first view if the user waits for progress bar to complete,the things in the next view will not work...
Store the returned NSTimer from scheduledWithTimeInterval::::: and later call invalidate on it to stop the timer.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0005 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
// This will stop the tmer
[timer invalidate];
To stop timer you need to use
[timer invalidate];
I have a NSTimer object.
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(timerAction) userInfo:nil repeats:YES];
[timer fire];
The method 'timerAction' repeats perfectly when call the timer from viewDidLoad method, but when I call the timer from parserDidEndDocument, the method 'timerAction' runs only once. Why is this?
you can try running the timer on the main thread.
Try this
create a new method that includes the code, to start timer, like :-
-(void)createTimer{
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(timerAction) userInfo:nil repeats:YES];
[timer fire];
}
In Your parserDidEndDocument delegate, try this:
[self performSelectorOnMainThread:#selector(createTimer) withObject:[nil waitUntilDone:YES]
How can i start an NSTimer when a user clicks a button?
EDIT:i had the code working and everything, but i put the code to hide the button above the NSTimer code, and when i placed it below the NSTimer code it worked!
The line of code to start a timer is (read the docs here) :
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:nil];
This will call the method timerFired: after 1 second i.e.
- (void)timerFired:(NSTimer *)timer {
NSLog(#"timer : %# has gone off", timer);
}
To get this to trigger from a button, you need this method in your .h file :
- (IBAction)buttonPressed:(id)sender;
and in your .m file, detect the button press like this :
- (IBAction)buttonPressed:(id)sender {
NSLog(#"Button's been pressed!");
}
Then, in interface builder connect the button's 'Toiuch Up Inside' action to this method.
Schedule it in the IBAction of the button. Check NSTimer Class Reference to know how to use timers.
You have to insert the command of[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:nil];
inside the buttonclick. Then only you could make the NStimer run after the button click (if you want to run the timer after the button click event )