Write code instead of selector in like NSTimers? - iphone

Is there anyway to write code instead of setting a selector to method to call in NSTimer?
If i want to print hello world after 5 sec i can do it like this.
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(helloWorld:) userInfo:nil repeats:NO];
and have this function
-(void)helloWorld:(NSTimer*)aTimer {
NSLog(#"Hello World!");
}
But instead of writeing functions for every timer you have is it possible to add NSLog(#"Hello World!") in the same line as where i create the timer?

I use this code– an NSTimer category:
https://gist.github.com/250662/d4f99aa9bde841107622c5a239e0fc6fa37cb179

Some methods allow you to pass a code block as a parameter. Unfortunately this isn't supported for NSTimer.

Related

How to toggle on/off timer programmatically

This is what I want to do:
I want a timer, to fire a method and then, in the end of this method, be toggled off, and turn on an other timer on another method, and then entering a loop.
So what are the codes used to toggle between on and off the timer on a method?
In Delphi I use:
timer.enable:=True; // timer.enable:=False;
Are there a similar way to do it on objective-c?
I'm using Xcode 4.4
Thanks!
To turn the timer off, call invalidate on your timer like so:
[yourTimer invalidate]
And then to start a new one:
NSTimer *newTimer;
newTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 //Every how many seconds
target:self
selector:#selector(methodToCall)
userInfo:nil
repeats:YES];
Assuming your NSTimer is called "timer", you can use...
[timer invalidate]
to stop the timer. To make a timer pass a message to it's target method instantly, use
[timer fire]
To start a timer, you use one of the constructor methods listed in the documentation (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html) such as
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(doThisWhenTimerFires:) userInfo:nil repeats:NO]
- (void)doThisWhenTimerFires:(NSTimer *)timer
{
//code here
}

call method every 5 minutes in my UIView

I want to call a certain method in my UIView code say every 5 minutes - how do I accomplish that?
You can use an NSTimer:
Put the following in your viewDidLoad (where 300 is the number of seconds):
[NSTimer scheduledTimerWithTimeInterval:300.0f
target:self
selector:#selector(updateMethod:)
userInfo:nil
repeats:YES];
And then create your update method:
- (void)updateMethod:(NSTimer *)theTimer {
// Your code goes here
}
You can use NSTimer to do this.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Specifically, the
timerWithTimeInterval:target:selector:userInfo:repeats:
class method.
There is an easy way to do this, in you update method
-(void)update{
//Your update methods here
[self performSelector:#selector(update) withObject:nil afterDelay:300.0];
}

is there timer in ios

I want to create a timer for example to count 2 sec and after each second an nslog type for example 1 sec passed
any suggestion to do that
yes there is NSTimer, use it as -
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector() userInfo:nil repeats:NO];
What you're after is NSTimer.
Which, I can't not point out, even a cursory search of the framework docs would have turned up. Laziness IS one of the three Virtues of the Programmer, but c'mon.
You can call your NSTimer like this
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(changeValue) userInfo:nil repeats:YES];
changeValue function can be like
-(void)changeValue{
NSLog("calling function after every two seconds");
}
You might want to use NSTimers timerWithTimeInterval:target:selector:userInfo:repeats:
method. Point that towards some object that implements a selector which prints your log entries.
Yes, ios having timer which is used for periodically call method or button touchup event. we can use it like following:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(Your_target_Method)
userInfo:nil
repeats:NO];
NSInteger timer=0;
now call method every 1 sec
-(void)Your_target_Method{
timer=timer+1;
NSLog(#"Timer:%ld",timer);
}
Furthermore, visit https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Timers.html

I want to call a method after particular time until my app opened in iphone?

I want to call a method after each 2 minutes,
how can I apply such logic?
Use an NSTimer object. It's been talked about quite a lot here, so I won't repeat all that - just search for "NSTimer" in the search box at the top right. :-)
You can use a timer..
[NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:#selector(yourFunction:) userInfo:nil repeats:YES];
.........
..........
-(void)yourFunction:(NSTimer*)timer{
//do your action here
}
I use a NSTimer to trigger a method. I use the below code in my initial viewDidLoad section:
myTimer = [NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:#selector(theMethod) userInfo:nil repeats:YES];
Hope that helps.

I should able to delay 2 seconds using NSTimer. How to do it?

I want to produce a delay of 2 seconds using NSTimer how to initialize timer in program?
Multiple options here.
If you just want a delay of 2 seconds you could use the sleep() function
#include<unistd.h>
...
sleep(2);
Or you may be able to use NSTimer like so
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(fireMethod) userInfo:nil repeats:NO];
And in your class you would have a method defined as
-(void)fireMethod
{
//Do stuff
}
Here you go...
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:#selector(action)
userInfo:nil
repeats:NO];
Simple answer: [NSThread sleepForTimeInterval:10.0];
Note that you should not really be thinking about delays in an event driven UI/OS. You should thinking about tasks you want to do now, and tasks you want to do later, and code these subtasks and schedule them appropriately. e.g. instead of:
// code that will block the UI when done in the main thread
- (void) methodC {
doA();
delay(2);
doB();
}
you might want to have code that looks more like:
- (void) methodA {
doA();
return; // back to the run loop where other useful stuff might happen
}
- (void) methodB {
doB();
}
and you can then schedule methodB with an NSTimer at the end of methodA, an NSTimer started by whatever called methodA, or, the best option, by the asynchronous completion routine of something started by methodA.