Possibilities to use NSTimer global - iphone

I am implementing an iPhone application. In that I need to use a timer to call a method to get the values frequently. And Also I need to stop the Timer in another method. So I need to use it as global. But I don't an idea to create a global Timer can you guys please help on this.
Thanks in Advance,
Sekhar Bethalam.

Just keep a reference to the timer.
In .h file:
NSTimer *timer;
..
#property (nonatomic, retain) NSTimer *timer;
In .m file:
#synthesize timer;
..
// Initiation:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFireMethod:) userInfo:nil repeats:YES];
..
// Termination:
[self.timer invalidate];

In the interface, declare a property:
#interface myViewController : UIViewController
{
NSTimer *timer;
}
#property (nonatomic, retain) NSTimer *timer;
#end
and then synthesise it in the .m file:
#implementation myViewController
#synthesize timer;
and then whenever you need to use it, just use timer.

Related

no known class method for selector 'scheduledTimerWithTimeInterval with timer

I have tried to figure out this, by reading other threads here - but I have not been able to work out anything so far.
If it's be just being a novice that doesn't understand the connections I need to make or if I doesn't have any relevance - I don't know.
I am making an iPhone app with 2 timers in it.
I am working on getting the source code for the timer to work.
I use a flip side application template from xcode, as I want to use the flip side for settings later on (where you can customize timers
In my MainViewController.h:
#import "FlipsideViewController.h"
#import "UIKit/UIKit.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate> {
IBOutlet UILabel *timerP1;
IBOutlet UILabel *timerP2;
NSTimer *myTimerP1;
NSTimer *myTimerP2;
}
- (IBAction)startP1;
- (IBAction)stopP1;
- (IBAction)resetP1;
- (void)showActivityP1;
- (IBAction)startP2;
- (IBAction)stopP2;
- (IBAction)resetP2;
- (void)showActivityP2;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#end
In my MainViewController.m:
#interface MainViewController ()
#end
#implementation MainViewController
- (IBAction)startP1{
myTimerP1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showActivityP1) userinfo:nil repeats:YES];
}
- (IBAction)stopP1{
[myTimerP1 invalidate];
}
- (IBAction)resetP1{
timerP1.text = #"60";
}
- (void)showActivityP1;{
int currentTimeP1 = [timerP1.text intValue];
int newTimeP1 = currentTimeP1 - 1;
timerP1.text = [NSString stringWithFormat:#"%d", newTimeP1];
}
- (IBAction)startP2{
}
- (IBAction)stopP2{
}
- (IBAction)resetP2{
}
- (void)showActivityP2{
}
I get the error "No known class method for selector 'scheduledTimerWithTimeInterval:target:selector:userinfo:repeats:'
What am I doing wrong here?
What am I doing wrong here?
Well, frankly what you're doing wrong is not using autocompletion.
It's a small typo, nothing to worry about.
userinfo should be userInfo (note the capital I)
So the complete call becomes:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(showActivityP1)
userInfo:nil
repeats:YES];
Oh and by the way - (void)showActivityP1;{...} will cause you troubles as well, just drop the ;

Switching from UIViewController to UITabBarController

Im trying to show a UIViewController for 2 seconds before the UITabBarController, i know i have to make it fromm my appdelegate. Ive tried by first assigning my self.window.rootviewcontroller to my UIViewController and with a scheduled timer after 2 seconds reassigning my self.window.rootviewcontroller to my UITabViewController.
The problem is that when i test it, my viewcontroller shows up, but after the 2 seconds the app crashes.
This is my LaMetro_88AppDelegate.h
#interface LaMetro_88AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIView *startupView;
NSTimer *timer;
UIViewController *LoadingViewController;
UITabBarController *tabBarController;
}
-(void)changeView;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet UIViewController *LoadingViewController;
#end
This is my LaMetro_88AppDelegate.m
#implementation LaMetro_88AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize LoadingViewController = _LoadingViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.LoadingViewController;
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeView:) userInfo:nil repeats:NO];
[self.window makeKeyAndVisible];
return YES;
}
-(void)changeView
{
self.window.rootViewController = self.tabBarController;
}
Your app is crashing because your selector has a colon after it (changeView:) whereas, the method does not. Just delete that colon. Also, there is no need to have a ivar for the timer or even to assign the timer creation to anything -- that line can just be:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeView) userInfo:nil repeats:NO];

Switch from UIViewController to a TabBarController [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Switching from UIViewController to UITabBarController
Im trying to show a UIViewController for 2 seconds before the UITabBarController, i know i have to make it fromm my appdelegate. Ive tried by first assigning my self.window.rootviewcontroller to my UIViewController and with a scheduled timer after 2 seconds reassigning my self.window.rootviewcontroller to my UITabViewController.
The problem is that when i test it, my viewcontroller shows up, but after the 2 seconds the app crashes.
This is my LaMetro_88AppDelegate.h
#interface LaMetro_88AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIView *startupView;
NSTimer *timer;
UIViewController *LoadingViewController;
UITabBarController *tabBarController;
}
-(void)changeView;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet UIViewController *LoadingViewController;
#end
This is my LaMetro_88AppDelegate.m
#implementation LaMetro_88AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize LoadingViewController = _LoadingViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.LoadingViewController;
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeView:) userInfo:nil repeats:NO];
[self.window makeKeyAndVisible];
return YES;
}
-(void)changeView
{
self.window.rootViewController = self.tabBarController;
}
Because the changeView method doesn't take a parameter, it's method signature doesn't include a colon. Change your selector to changeView without the colon:
#selector(changeView)
Change this:
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeView:) userInfo:nil repeats:NO];
To this:
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changeView) userInfo:nil repeats:NO];

Activating iPhone alarm and making sound

First post and first iPhone app in the making here, so please excuse the any n00b mistakes. I'm having trouble activating the alarm. I have a Datepicker and a button as the crux of the code. Here's the .h file
#interface Alarm : UIViewController
#property (retain, nonatomic) NSDate *alarm;
#property (retain, nonatomic) IBOutlet UIButton *sleepButton;
#property (retain, nonatomic) NSTimer *timer;
#property (retain, nonatomic) IBOutlet UIDatePicker *timePicker;
- (IBAction)startAlarm:(id)sender;
- (void)checkAlarm;
#end
And here is the .m file.
- (IBAction)startAlarm:(id)sender
{
self.alarm = [self.timePicker date];
self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:#selector(checkAlarm) userInfo:nil repeats:YES];
{
- (void)checkAlarm
{
// alarm is reached
if ([NSDate date] == self.alarm)
{
// rest of code goes here
}
{
I also have no idea how to make sound and make it repeat itself, so if I can get some guidance on how to do that, that would be great. Thanks!
The problem is here:
if ([NSDate date] == self.alarm)
Dates have millisecond resolution, and your timer only fires once per second - the chances that they align perfectly is rather slim.
Instead you should check:
if ([[NSDate date] compare: self.alarm] == NSOrderedDescending)
to check if the alarm date was passed.

Problem with NSTimer

Edit2: Why only progress got updated in "doSomething" method but not point0?
Edit: with the code I have. I know I must overlook something, but I just could not find it.
I am writing an iphone app which uses NSTimer to do some tasks. In the program, I could not get the updated value of the variable updated inside NSTimer loop. Here is my code.
Interface File
import
#interface TestNSTimerViewController : UIViewController {
IBOutlet UIProgressView *progress;
IBOutlet UIButton *button;
IBOutlet UILabel *lable1;
IBOutlet UILabel *lable2;
NSTimer *timer;
float point0;
}
#property (nonatomic, retain) UIProgressView *progress;
#property (nonatomic, retain) UIButton *button;
#property (nonatomic, retain) NSTimer *timer;
#property (nonatomic, retain) UILabel *lable1;
#property (nonatomic, retain) UILabel *lable2;
- (IBAction)buttonClicked:(id)sender;
#end
Implementation file
#import "TestNSTimerViewController.h"
#implementation TestNSTimerViewController
#synthesize progress;
#synthesize button;
#synthesize lable1;
#synthesize lable2;
#synthesize timer;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)buttonClicked:(id)sender {
point0 = 1.0f;
lable1.text = [NSString stringWithFormat:#"%3.1f",point0];
timer = [NSTimer scheduledTimerWithTimeInterval:0.05
target:self selector:#selector(doSomething) userInfo:nil repeats:YES];
lable2.text = [NSString stringWithFormat:#"%3.1f",point0];
}
- (void)doSomething {
progress.progress = progress.progress+0.1;
point0 = 2.0f;
if (progress.progress == 1.0) {
[timer invalidate];
}
}
- (void)dealloc {
[button release];
[progress release];
[lable1 release];
[lable2 release];
[timer release];
[super dealloc];
}
#end
After the NSTimer loop, I checked the value of point0. It did not change the value to 2.3. What's wrong with the code?
Thank you,
From the Reference document
Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes and releases the timer, either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.
The timer you use is a Repeating timer, so you should not invalidate it at all. Or use the following Line, because the timer needs to fired each time the button is clicked. I have set the repeat parameter to NO.
timer = [NSTimer scheduledTimerWithTimeInterval:0.05
target:self selector:#selector(doSomething) userInfo:nil repeats:NO];
- (void)buttonClicked:(id)sender {
point0 = 1.0f;
lable1.text = [NSString stringWithFormat:#"%3.1f",point0];
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05
target:self selector:#selector(doSomething) userInfo:nil repeats:NO];
lable2.text = [NSString stringWithFormat:#"%3.1f",point0];
}
Then in doSometing function:
- (void)doSomething {
progress.progress = progress.progress+0.1;
point0 = 2.0f;
if (progress.progress < 1.0) {
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05
target:self selector:#selector(doSomething) userInfo:nil repeats:NO];
}
}
I think you should reset the progress variable at some point.
I found the answer. label2.text line is executed before NSTimer finishes the run loop. I need to rewrite my code such that it waits until NSTimer finishes the run loop.