NSTimer method calls multiple times - iphone

I am developing one application,in that i have one requirement.onTyping method calls on textfield text change.when i enter first letter on textfield ontying method get called and timer starts &pause method called after 10 sec,pause method get called on continue on typing.But i want call pause method on last letter typing.how can i achieve this one
-(IBAction)onTyping:(id)sender
{
// [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:#selector(pause) userInfo:nil repeats:NO];
if(timer==nil)
{
timer=[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(pause) userInfo:nil repeats:NO];
}
[self fadeIn];
}
-(void)pause
{
[self fadeOut];
}
- (void)fadeIn
{
NSlog(#"fadeIn");
}
- (void)fadeOut
{
NSlog(#"fadeOut");
}

call that method textFieldShouldEndEditing: method of UITextFieldDelegate like bellow...
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[self pause];
return YES;
}

Related

Detect Pause when user stop type in UISearchBar

I have the following UISearchbar code:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSTimer *myTimer;
NSLog(#"Timer=%#",myTimer);
if (myTimer)
{
if ([myTimer isValid])
{
[myTimer invalidate];
}
myTimer=nil;
}
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(OnTextChange) userInfo:nil repeats:NO];
}
after writing above code my log shows Timer=null,
OnTextChange is method which fetches data from Url.
Thanks in advance !!
Please write this line in .h file
NSTimer *myTimer;
and then do it in .m file
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(#"Timer=%#",myTimer);
if (myTimer)
{
if ([myTimer isValid])
{
[myTimer invalidate];
}
myTimer=nil;
}
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(OnTextChange) userInfo:nil repeats:NO];
}
every time you are creating new object for NSTimer that's why it gives null.
declare NSTimer *myTimer globally.
You could use an NSTimer to delay the invocation of your search, and cancel the timer whenever the text changes: Give your object an NSTimer property or field; in searchBar:textDidChange: cancel any existing timer, then create and schedule a new one. The target of the timer should call your search method.
Refer this duplicate question: UISearchBar detect when user stop type and don't search immediately so detect pause

On click want to perform two actions

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];
}

NSTimer causes crash

The following method causes a crash. The UI is like a button, which handles the start / stop functionality of the NSTimer. If the timer runs, a UILabel is updated. Using the viewDidLoad Method makes my timer work, stopping it works too, but starting it again crashes the app.
Removing the alloc in the viewDidLoad method and trying to use the start button causes a crash instantly. Even the NSLog(#"Start now");is not called.
Code:
- (void)tick {
NSLog(#"tick");
float value = [moneyLabel.text floatValue];
moneyLabel.text = [NSString stringWithFormat:#"%f", value + 1.0];
}
- (IBAction)startStopButtonClicked:(UIButton *)sender {
if ([sender.titleLabel.text isEqualToString:#"Start"]) {
NSLog(#"Start now");
if (timer) {
NSLog(#"Timer valid");
[timer fire];
} else {
NSLog(#"Timer is nil");
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(tick) userInfo:nil repeats:YES];
[timer fire];
}
NSLog(#"bla");
[sender setTitle:#"Stop" forState:UIControlStateNormal];
} else {
[timer invalidate];
timer = nil;
NSLog(#"Stopped.");
NSLog(#"Timer isValid: %#", timer);
[sender setTitle:#"Start" forState:UIControlStateNormal];
}
}
I don't see the need to call [NSTimer fire] at all; it should be enough to allow the timer to decide when to fire.
Firstly ensure that timer is nil (it should be if it's an instance variable of the object), although explicitly setting it to nil in - (id)init won't hurt.
Next I would use the state of the timer itself to determine whether start/stop has been pressed, not the text in the button:
- (IBAction)startStopButtonClicked:(UIButton *)sender
{
if (timer != nil)
{
NSLog(#"Stopping timer");
[timer invalidate];
timer = nil;
}
else
{
NSLog(#"Starting timer");
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(tick)
userInfo:nil
repeats:YES];
}
[sender setTitle:(timer != nil ? #"Stop" : #"Start")
forState:UIControlStateNormal];
}
The code you have posted works as desired - just tested it in a new project, so the problem could be somewhere else. I tested it only by declaring the ivar NSTimer *timer; without any initialization in viewDidLoad: or the designated initializers...

NSTimer fails to invoke methord

Following is my sample code.
#interface TrackTimer : NSObject {
NSTimer *timer;
}
#property (nonatomic, retain) NSTimer *timer;
- (void) startTimer;
- (void) stopTimer;
- (void) timerFired;
#end
TrackTimer.m
#synthesize timer;
- (void) startTimer
{
NSLog(#"Timer started ...");
if(timer)
{
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(timerFired) userInfo:nil repeats:NO];
}
- (void) stopTimer
{
NSLog(#"Timer stoped ...");
[tTimer invalidate];
}
- (void) timerFired
{
NSLog(#"Timer Fired ... :)");
}
I have to use the same timer object from 3 different view controllers, my problem is startTimer method do not invoke timerFired method in 2nd UIViewController. Its works perfectly on 1st and 3rd View Controller.
appln Flow : 1stView -> 2ndView -> 3rdView
You are doing everything right... almost.
Your timer does not fire, because of the "if" statement.
if (timer) {
timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(boom) userInfo:nil repeats:NO];
}
Here, the "if" statement returns NO, because the timer is not yet initialized..
The fact that you make it a property and synthesize it does not mean that (timer != nil)
If you remove the "if" statement it should work...
From the Apple docs on NSTimer:
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
So, it looks like the signature of your timerFired method needs to be expanded to include one parameter '(NSTimer*)theTimer' and your selector needs to be #selector(timerFired:)
Don't really know how you do that, but NStimer has a class method called
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats. So you can do it like this:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:#selector(timerFired)
userInfo:nil
repeats:YES];
This will invoke the timerFired method for you.
P.S.Here's the link to a simple app that does just what you want.
http://www.mediafire.com/?8uz115drqzb2nan

How can I make a counter?

How can I make a counter from 0 to 10 choosing the delay?
you need to use NSTimer,
Check the below code as reference.
- (void) startCounter {
counterCount = 0;
[NSTimer scheduledTimerWithInterval:0.09f target:self selector:#selector(showElapsedTime:) userInfo:nil repeats:YES];
}
showElapsedTime will be called after delay, you provide.
-(void) showElapsedTime: (NSTimer *) timer {
counterCount++;
if(counterCount == 10)
{
[timer invalidate];
}
// Write your code here
}
Create an NSTimer:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats;
Yours would probably look like this:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
In the method that is invoked, increment a counter, and check to see if you've hit your count to stop the timer:
- (void)timerFired:(NSTimer *)timer
{
static int counter = 0;
// Do Something
counter++;
if(counter == 10)
[timer invalidate];
}
The method [performSelector: withObject: afterDelay] of the NSObject class is normally used for timed operations.