no known class method for selector 'scheduledTimerWithTimeInterval with timer - iphone

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 ;

Related

Change NSTime with respect to the value of UISlider

There are lots of questions and answers about how to change the UISlider value with NsTimer but i need to create the reverse condition i need to change the NSTimer interval per UISlider value change, and want the slider value work in reverse manner. so can anyone help to achieve this?
Thanks for your approaches and time :)
you can set Timer set again interval value at the IBAction of UISlier Method like:-
.h class:-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UILabel *lblTimer;
IBOutlet UISlider *timeSlider;
}
#property(nonatomic,strong)NSTimer *timer;
.m class:-
- (void)viewDidLoad
{
[super viewDidLoad];
_timer=[[NSTimer alloc]init];
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateLable) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
int i=0;
-(IBAction)changeSlider:(UISlider*)sender
{
NSLog(#"%d",(int)sender.value);
[_timer invalidate];
_timer=[[NSTimer alloc]init];
_timer=nil;
_timer = [NSTimer scheduledTimerWithTimeInterval:(int)sender.value target:self selector:#selector(updateLable) userInfo:nil repeats:YES];
// i=0;
//text_field .text= [[NSString alloc] initWithFormat:#" Value %d ", (int)slider.value];
}
-(void)updateLable
{
[lblTimer setText:[NSString stringWithFormat:#"%d",i]];
i++;
}
OUTPUT OF CODE

Passing NSTimer has parameter

I have method which take NSTimer has parameter which is in Class A
-(void)demoMethod:(NSTimer *)timer{
//Do something!
}
Now I have covering test case for the method:
-(void)testDemoMethodPassNilTimer{
//Created class Instance for the ClassA
ClassA *testA = [[ClassA alloc]init];
//[test testDemoMethod:nil];
STAssertThrows([testA testDemoMethod:nil],#"should throw exception");
}
-(void)testDemoMethodPassTimer{
ClassA *testA = [[ClassA alloc]init];
STAssertNoThrows([testA testDemoMethod:??????]);
}
What should be the proper way to pass the parameter for NSTimer Object for the test Case method testDemoMethodPassTimer?
Give it an actual timer object, but don't schedule it on the run loop.
It was also my need to parse parameter to NSTimer. So I had come up with creation of category class, which helped me.
Here are the steps to create Category class over NSTimer.
Right click on project and select "New File"
Cacoa Touch > Objective-C category > Next
Give Category : additions & Category on : NSTimer
in .h file just create property of placeId
in .m synthesize it using #dynamic & create setter, getter
import that .h file in you file.
NSTimer+additions.h file
#import <Foundation/Foundation.h>
#interface NSTimer (additions)
#property(nonatomic,retain) NSString *additionalTag;
#end
NSTimer+additions.m file
#import "NSTimer+additions.h"
#import <objc/runtime.h>
NSString *const additionalTagKey = #"additionalTagKey";
#implementation NSTimer (additions)
#dynamic additionalTag;
- (void)setAdditionalTag:(NSString*)aObject
{
objc_setAssociatedObject(self, additionalTagKey, aObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString*)additionalTag
{
return objc_getAssociatedObject(self, additionalTagKey);
}
#end
Now #import "NSTimer+additions.h" in your class using NSTimer
Passing parameter to NSTimer
-(void)testDemoMethodPassTimer{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(timerTick:)
userInfo:nil
repeats:YES];
timer.additionalTag = #"parameter passed to timer";
ClassA *testA = [[ClassA alloc]init];
STAssertNoThrows([testA testDemoMethod:timer]);
}
Retrieving parameter from timer
-(void)demoMethod:(NSTimer *)timer{
NSLog(#"My Parameter: %#",timer.additionalTag);
}
Many Steps are involved but hopefully help you a bit. :)

App crashes when I add a Activity indicator

I'm writing an app and ind the app I got a Web View and I want to add an Activity Indicator. So I use this code:
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
target:self selector:#selector(loadingPage) userInfo:nil repeats:YES];
But when I insert this code my app crashes and refers me to this code:
main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
at the line that starts with "int retVal..." it tells me "Thread 1: Program recieved signal: "SIGABRT"". By the way this code that it shows me is not something I wrote it was in the Xcode project from the beginning.
So my question is: Does anyone know a simple way of fixing this or another simple way of adding a Activity Indicator?
If someone knows a better way pleace write the code since I'm new to app development and still have allot to learn.
In advance thank you very much :)
Here's some more code:
.h file
#interface Ugeblad : UIViewController {
// Web View
IBOutlet UIWebView *faktaPDF;
// Activity Indicator View
IBOutlet UIActivityIndicatorView *pageLoadingIndicator;
NSTimer *timer;
}
#property (nonatomic, retain) IBOutlet UIWebView *faktaPDF;
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *pageLoadingIndicator;
#end
.m file
[faktaPDF addSubview:pageLoadingIndicator];
[faktaPDF loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"Website"]]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loadingPage) userInfo:nil repeats:YES];
If by activity indicator you mean a spinning wheel (known as the UIActivityIndicatorView), you don't need a timer for it. It animates on its own; you just have to place it on the view and call [startAnimating].
Don't use an NSTimer for this, that's gross. Assign yourself as the delegate of the UIWebView and implement
-webViewDidStartLoad: to present your "loadingView" and then implement
-webView:didFinishLoad: and -webView:didFailLoadWithError: to hide your "loadingView".

Possibilities to use NSTimer global

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.

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.