Pause NSTimer to go about screen - objective-c

i have a slideshow that works with NSTimer function calls every 3 seconds and i have a About button when user touch about that goes to another screen:
-(IBAction)toAbout:(id)sender
{
about *ab=[[about alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:ab animated:YES];
[ab release];
}
and returns from about screen with this function
-(IBAction)aboutButton:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
why I can pause the NSTimer when I'm go to about screen?
and why I can creat the object that works in slideshow screen and works in about screen too?

There is no method for pausing your timer and it doesn't make sense to call [timer setFireData:[NSDate distantFuture]]. Instead, invalidate the timer and create a new one once entering the first view.
Invalidate by saying [timer invalidate]; and set the timer to nil timer = nil;.

To pause the timer you may simply call
[timer setFireDate:[NSDate distantFuture]];
This does not invalidate the timer but simply avoid that the timer keeps firing.

Related

How to stop sending requests to dispatch_async requests for ringing?

I click the button to beep and as the background thread there is a welcome message appears on the view controller, the beep is continuously going on until i close the application.
How can i suspend the queue for further execution when welcome message arrives on the view controller?
I tried calling from another view controller dispatch_suspend(dispatch_get_main_queue()); when welcome screen comes in but the beep is continuously going on in background. Can somebody have hint to stop queue from executing or just clean the queue?
dispatch_async(dispatch_get_main_queue(), ^{
NSInteger timeintervalForBeep;
if (_bu.buModel == BuTwo) {
timeintervalForBeep = 7;
} else {
timeintervalForBeep = 2;
}
_findBeepTimer = [NSTimer scheduledTimerWithTimeInterval:timeintervalForBeep
target:self
selector:#selector(makeBeep)
userInfo:nil
repeats:YES];
// Fire it once immediately.
[_findBeepTimer fire];
});
in the makeBeep function, invalidate the timer by adding the following codes:
[_findBeepTimer invalidate];
_findBeepTimer = nil;

Invalidate Timers when user press button home, doesn't work

I have this code in app delegate that invokes an existing method in my view that invalidate timers and pause the music of my application:
Appdelegate.m
- (void)applicationWillResignActive:(UIApplication *)application{
GameViewController *gamer = [[GameViewController alloc] init];
[gamer EnterBackground];
}
GameViewController.m
-(void)EnterBackground{
NSLog(#"App Enter in background mode!");
[t1 invalidate];
[t2 invalidate];
[t3 invalidate];
[_audioPlayer stop];
}
I can not understand why the simulator when I press the lock button or home, the NSLog works, but timers and the audio still continue to run, why this is happening? there is a possible solution?

NSTimer Will Not Invalidate

-(IBAction) loadWeb: (id) sender {
[_webView loadRequest:nsrequest2];
_webView1.hidden = YES;
_webView.hidden = NO;
self.checkForAdd = [NSTimer scheduledTimerWithTimeInterval:0.4
target:self selector:#selector(checkForAddToCart:) userInfo:nil
repeats:YES];
}
-(IBAction)button1:(id)sender {
[self.checkForAdd invalidate];
}
How would I invalidate the timer? I have tried it without self and many other ways, but for some reason, when I press the button the timer does not invalidate.
Check if loadWeb: is called multiple times. If it is, you will have old timers running without having a reference to them so you can't invalidate them. You should have [self.checkForAdd invalidate]; before you create the new timer.
When you do invalidate the timer, if you aren't creating a new one, set self.checkForAdd = nil; to be sure you aren't going to try using the timer again (some actions will throw an exception if you do).
If at any point you do self.checkForAdd = nil; without having invalidated the timer then you won't have a reference to it so you won't be able to invalidate it in the future.
According to your code, loadWeb is being trigger by a button. So you will be creating new timer every time when button will be press. Its better you create timer on some where else, like create in init, or in viewDidLoad method, because if you are creating this here, you have to make sure you are not creating timer again and again. You can do this by doing a if check
if(!self.checkForAdd){
self.checkForAdd = [NSTimer scheduledTimerWithTimeInterval:0.4
target:self selector:#selector(checkForAddToCart:) userInfo:nil
repeats:YES];
}
Right now its creating new timer on button tap, and previous one have no reference.

Update UILabel by timer doesn't work

I have UIView with Label that shows the time like stopwatch. I set the label from a Timer method. Here is the code below:
ivarTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
My update method looks like this:
- (void)updateTimer {
...
self.myLabel.text = #"someChangedStr";
}
It works great. But on the view I have UITableView and If I scroll the TableView, self.myLabel.text stops update. Should I use threads or something else?
You could run your timer on another thread, but you must always do UI updates on the main thread.
However, try adding your NSTimer to the UITrackingRunLoopMode so it will still fire when tracking touches on your window.
[[NSRunLoop mainRunLoop] addTimer:ivarTimer
forMode:UITrackingRunLoopMode];

Creating an iOS Timer

I am trying to create a "stop watch" type functionality. I have one label (to display the elapsed time) and two buttons (start and stop the timer). The start and stop buttons call the startTimer and stopTimer functions respectively. Every second the timer fires and calls the increaseTimerCount function. I also have an ivar timerCount which holds on to the elapsed time in seconds.
- (void)increaseTimerCount
{
timerCountLabel.text = [NSString stringWithFormat:#"%d", timerCount++];
}
- (IBAction)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(increaseTimerCount) userInfo:nil repeats:YES];
}
- (IBAction)stopTimer
{
[timer invalidate];
[timer release];
}
The problem is that there seems to be a delay when the start button is pressed (which I am assuming is due to reinitializing the timer each time startTimer is called). Is there any way to just pause and resume the timer without invalidating it and recreating it? or a better/alternate way of doing this?
Thanks.
A bit dated but if someone is still interested...
don't "stop" the timer, but stop incrementing during pause, e.g.
- (void)increaseTimerCount
{
if (!self.paused){
timerCount++
}
timerCountLabel.text = [NSString stringWithFormat:#"%d", timerCount];
}
You can't pause the timer without using invalidate. What you can do is add
[timer fire];
after you create the timer in startTimer.