Fire IBAction automatically - objective-c

I have a button and want to fire it automatically by itself without touch.
Is it possible?
-(IBAction)xxx:(id)sender

My answer assumes you have the method:
- (IBAction)someAction:(UIButton *)sender {
}
and that you have a reference to the button in an instance variable named someButton.
If you just need to "fire it" now, simply call it:
[self someAction:someButton];
If you need to "fire it" once, but later, you can do:
// call it 5 seconds from now
[self performSelector:#selector(someAction:) withObject:someButton afterDelay:5.0];
If you want to fire it repeatedly, use a timer:
myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(buttonTimerFired) userInfo:nil repeats:YES];
- (void)buttonTimerFired {
[self someAction:someButton];
}

Action can be called like every regular function - you can do it by running a timer on something else.

You should use NSTimer for doing your work.
[NSTimer scheduledTimerWithTimeInterval: 0.01f target: self selector: #selector(BtoonMethod) userInfo: nil repeats: NO];
-(void)BtoonMethod
{
// write code for call yor button method
}

Related

Why memory consumption grows?

Here is a very simple piece of code:
- (void)functionOne
{
[self performSelector:#selector(functionTwo) withObject:nil afterDelay:1.0];
}
- (void)functionTwo
{
[self performSelector:#selector(functionOne) withObject:nil afterDelay:1.0];
}
As you can see there's nothing in these two methods what could cause growth of memory consumption. But it grows. Very slowly, but it does. About 0.01 MB every three seconds. Why? How can i avoid it?
You are effectively creating an infinite loop. If you want to switch the state of an object every one second (as you said in the comments), do it this way:
Create a method like so:
- (void)functionOne
{
if( [obj isEqual:stateA] ) {
obj = stateB;
} else {
obj = stateA;
}
}
and call it with a timer:
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:#selector(functionOne) userInfo:nil repeats:YES];
to avoid infinite loops you should use a NSTimer and one function, in which you toggle the state of your object.
in your init method, or something like viewDidLoad you should start a timer like
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(switchState:)
userInfo:nil
repeats:YES];
}
then you use one method like
- (void) switchState:(NSTimer *)timer {
if ([[self yourState] isEqual:stateOne]) {
[self setYourState:stateTwo];
} else {
[self setYourState:stateOne];
}
}
for more information see http://developer.apple.com/library/ios/#documentation/cocoa/reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
The memory grows simply because the Application needs to hold onto some state. Each time you call perform selector that selector is pushed onto the stack. This stack is saved in memory. Hence the growth.

How to stop NSTimer started on button click in this timer callback method

I am having problems with stoping NSTimer started on button click. [timer invalidate] and timer = nil; just do nothing neither when I am trying to stop it viewWillDisappear nor in method invoked by method which is being invoked by this timer. However when I start my timer in viewWillAppear and invalidate it in viewWillDisappear everything is fine.
I suppose there might be an issue in thread I am starting the timer from. Can you help with that?
I looked through all answers here regarding NSTimer not stopping, but they didn't help to solve the problem.
The way I initialize my timer:
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(oneSecondPassedSinceRoundStarted) userInfo:nil repeats:YES];
The ways I tried to stop it:
[self.timer invalidate];
[timer invalidate];
It's funny how quick you can answer your own question after you asked for a help. No matter how long you have been struggling to find the answer before by yourself:
[self performSelectorOnMainThread:#selector(stopTimer) withObject:nil waitUntilDone:YES];
And the selector:
- (void) stopTimer
{
[timer invalidate];
timer = nil;
}
I have created and tested this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(oneSecondPassedSinceRoundStarted:) userInfo:nil repeats:YES];
}
-(void)oneSecondPassedSinceRoundStarted:(NSTimer *)time {
// Do what You want
NSLog(#"CALLING!");
}
-(IBAction)buttonAction:(id)sender {
NSLog(#"Stop timer!");
[myTimer invalidate];
myTimer = nil;
}
And it is working nice.
Note: You forget to add colon after oneSecondPassedSinceRoundStarted.

How can I reset a timer?

I have an app, that every 3 seconds, fires an event that goes to my server and grabs information. I would like to reset the timer count down if an event has transpired in between the time the timer last fired and when it fires subsequently.
So essentially, if my other event fires at 2.5 seconds, and the timer is set to fire in .5 seconds, I would like to reset the timer back to 3 seconds. How can I accomplish this?
I declare the timer as:
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: #selector(getUpdates)
userInfo:nil
repeats: YES];
And psuedo:
-(void) anEventHappened
{
// I got something from the server, I don't need to query it. Reset timer here.
}
-(void) getUpdates
{
// I received no reset, I should check for an update.
}
Does this work?
[_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: #selector(getUpdates)
userInfo:nil
repeats: YES];

Cocoa Touch - Timers

How can I make a timer that counts down from 3 and then runs a method? How would I do that?
Is that different from a timer counting from 0 to 3? It will still wait three seconds, either way.
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(myMethod:) userInfo:nil repeats:NO];
Better way might be to use performSelector:withObject:afterDelay: method:
[self performSelector:#selector(myMethod) withObject:nil afterDelay:3.0f];
Or in case method takes 1 parameter:
[self performSelector:#selector(myMethod:) withObject:parameter afterDelay:3.0f];
If method takes multiple parameters you'll need to use NSInvocation class
- (void) handleTimer: (NSTimer *) timer
{
do some work here...
} // handleTimer
// at some point in your controller
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 3.0
target: self
selector: #selector(handleTimer:)
userInfo: nil
repeats: NO];

Delayed call, with possibility of cancelation?

How do I trigger a delay, let's say I want to call a method (once) in 3 seconds from now, and how do I cancel that call if I need to?
You can also use -[NSObject performSelector:awithObject:afterDelay:], and +[NSObject cancelPreviousPerformRequestsWithTarget:selector:object].
Use NSTimer. Use this to set up a call to method in three seconds time. It will only be called once:
[NSTimer scheduledTimerWithTimeInterval: 3
target: self
selector: #selector(method:)
userInfo: nil
repeats: NO];
method needs to look like this:
- (void) method: (NSTimer*) theTimer;
You can pass parameters into the method using userInfo (set to nil in the above example). It can be accessed in the method as [theTimer userInfo].
Use the invalidate method on NSTimer to cancel it.
in your header..
NSTimer *timer;
when you want to setup..
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(yourMethod:) userInfo:nil repeats:NO];
when you want to cancel..
[timer invalidate];