NSTimer strange problem - iphone

I have 2 ViewController(RootViewController is UITabBarController, ViewController1 links to the item of UITabBarController)
in RootViewController
-(void)startTimer:(NSInteger)v;
{
[NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
}
- (void)timerFired:(NSTimer *)timer {
[vViewController1 doSomething];
}
in ViewController1
-(void)doSomething;
{
//I set breakpoint but never be fired
}
timerFired is activated but the function doSomething in ViewController1 never been fired.
Welcome any comment
Thanks
interdev

If you know for a fact that timerFired: is being called, then the only reason that doSomething won't get called is if vViewController1 is nill. Check that again.

The self in scheduledTimerWithTimeInterval target:self means RootViewController, so it will only invoke doSomething in RootViewController but not in ViewController1.

Related

NSTimer not stopping?

I'm trying to to stop an NSTimer with the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
timer3 = [NSTimer timerWithTimeInterval:5.0 target:self selector:#selector(start) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer3 forMode:NSDefaultRunLoopMode];
}
-(void)invalidate
{
[timer3 invalidate];
timer3 = nil;
}
and I call -(void)invalidate from another class like this:
-(void)timer
{
ClassOfMyTimer *class = [[ClassOfMyTimer alloc] init];
[class invalidate];
}
but the timer doesn't stop. Does anyone know what I'm doing wrong?
You need to call your invalidate method on the same instance of your class that created the timer. In your timer method you create a new instance of your class which could have its own timer and invalidate that.
I'm kind of confused by what you're trying to do here, but I'd guess that you're not maintaining a reference to timer3.
Have you created a property in the .h file for the timer:
#property (strong) NSTimer *timer3;
And then added a synthesize statement in the .m file:
#synthesize timer3;
Then, in viewDidLoad:, you can maintain a reference to the timer you're creating via:
self.timer3 = [[[NSTimer timerWithTimeInterval:5.0 target:self selector:#selector(start) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer3 forMode:NSDefaultRunLoopMode];
And, to invalidate the timer later:
[self.timer3 invalidate]
self.timer3 = nil
On preview, Sven also has a valid solution to an issue that might be impacting you..

NSTimer Delegate Selection

I am trying to create a framework for all of the custom objects and views that I have made and use often, by creating custom delegate classes and custom objects. Everything has gone well except when trying to get NSTimers to call the correct method inside of the delegate class.
Here is the basic setup.
-(void) startTimers {
NSTimer *timer1 = [NSTimer timerWithTimeInterval:5 target:self selector:#selector(doSomething:) userInfo:nil repeats:YES];
NSTimer *timer2 = [NSTimer timerWithTimeInterval:5 target:self selector:#selector(doSomethingElse:) userInfo:nil repeats:YES];
}
I can easily just call this method and whatever, but when this time fires it does not call the method I defined as the selector. I am pretty sure it has something to do with the delegate value and which class it is making as the delegate.
Note the file I am writing in is a subclass of UIView, which is set up to be a delegate using the #protocol tags and all of that.
What should I set as the target when defining my timers to get them to call the correct methods.
EDIT:
Here is an example of what I am doing:
ExampleView.h
#import <UIKit/UIKit.h>
#protocol ExampleViewDelegate;
#interface ExampleView : UIView {
NSTimer *timer;
}
-(void) initWithStuff:(id)stuff andFrame:(CGRect)frame;
-(void) testTimer;
#end
#protocol ExampleViewDelegate
-(void) someDelegateFunction;
#end
ExampleView.m
#import "ExampleView.h"
#implementation ExampleView
-(id) initWithStuff:(id)stuff andFrame:(CGRect)frame {
self = [super initWithFrame:frame];
timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(testTimer) userInfo:nil repeats:YES];
return self;
}
-(void) testTimer {
NSLog(#"Timer Fired");
}
#end
If you add this custom view into a viewcontroller it will never call that testTimer function and print "Timer Fired" So what I am thinking is that when I set the delegate for this timer, it is actually setting it to something else. Any ideas?
NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(doSomething:) userInfo:nil repeats:YES];
Notice the method is called "scheduledTimerWithTimeInterval"

switching from view 1 to view 2 without pressing any buttons

How to switch from view1 to view 2 without having any button pressed. In my views i have is uiimageview and uitextview
With the NSTimer i m trying to do this
in the viewdidload method by using the following code:
In the firstviewcontroller.h file
#interface FirstViewController : UIViewController
{
NSTimer *SwitchingTimer;
}
In the firstviewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
SwitchingTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(SwitchView) userInfo:nil repeats:YES];
}
-(void)SwitchViews:(id)sender
In the secondviewcontroller.m file
-(void) SwitchView
{
SecondViewController *SecondView = [[SecondViewController alloc]
initWithNibName:#"SecondViewController" bundle:nil];
SecondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:SecondView animated:YES];
[SwitchingTimer invalidate];
self.SwitchingTimer = nil;
}
but nothing is happening. Can someone please tell me what i m missing in my code. Will appreciate help.
Thanks in advance.
There are a few issues in your code that are worth mentioning though I am not sure if those will provide you a solution.
Why do you want to repeat the timer every 2 seconds. I think you just want to switch to next view only once and if so then dont repeat the timer. So no need to invalidate the timer.
Your code for the SwitchView method is leaking memory. Please make sure that the SecondView is released after presenting the modal view(in case you are not using ARC).
Please follow the standard naming conventions. For eg: methods and variables should start with lowercase.
Regarding your issue please make sure that the nib name is correct and you are getting a valid object for the second view controller. You can check by using NSLog. Also ensure that the method Switchview is called. Try putting a break point and verify that it is called.
Another Option
If you just want to switch the view only once you can go for another option which does not make use of the NSTimer. For this, you can use performSelector:withObject:afterDelay:. This is just another option for the scenario I mentioned above.
You need to add it to the run loop:
- (void)viewDidLoad
{
[super viewDidLoad];
SwitchingTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(SwitchView) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer: SwitchingTimer forMode:NSDefaultRunLoopMode];
}
Also, you could rename -(void)SwitchView to -(void)switchViewTimerDidFire:(NSTimer *)timer. From the Documentation:
[...] The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
The timer passes itself as the argument to this method.

NSTimer fails to invoke methord

Following is my sample code.
#interface TrackTimer : NSObject {
NSTimer *timer;
}
#property (nonatomic, retain) NSTimer *timer;
- (void) startTimer;
- (void) stopTimer;
- (void) timerFired;
#end
TrackTimer.m
#synthesize timer;
- (void) startTimer
{
NSLog(#"Timer started ...");
if(timer)
{
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(timerFired) userInfo:nil repeats:NO];
}
- (void) stopTimer
{
NSLog(#"Timer stoped ...");
[tTimer invalidate];
}
- (void) timerFired
{
NSLog(#"Timer Fired ... :)");
}
I have to use the same timer object from 3 different view controllers, my problem is startTimer method do not invoke timerFired method in 2nd UIViewController. Its works perfectly on 1st and 3rd View Controller.
appln Flow : 1stView -> 2ndView -> 3rdView
You are doing everything right... almost.
Your timer does not fire, because of the "if" statement.
if (timer) {
timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(boom) userInfo:nil repeats:NO];
}
Here, the "if" statement returns NO, because the timer is not yet initialized..
The fact that you make it a property and synthesize it does not mean that (timer != nil)
If you remove the "if" statement it should work...
From the Apple docs on NSTimer:
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer
So, it looks like the signature of your timerFired method needs to be expanded to include one parameter '(NSTimer*)theTimer' and your selector needs to be #selector(timerFired:)
Don't really know how you do that, but NStimer has a class method called
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats. So you can do it like this:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:#selector(timerFired)
userInfo:nil
repeats:YES];
This will invoke the timerFired method for you.
P.S.Here's the link to a simple app that does just what you want.
http://www.mediafire.com/?8uz115drqzb2nan

How to refresh webview every 5 seconds

How can I refresh or reload webview every 5 seconds?
You can use NSTimer to call the method every 5 seconds and use -reload method of UIWebview
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(updateWeb) userInfo:nil repeats:YES];
-(void)updateWeb
{
[webView reload];
}
you need to use `NSTimer`,
Check the below code as reference.
- (void) startTimer {
[NSTimer scheduledTimerWithInterval:5.0f target:self selector:#selector(showElapsedTime:) userInfo:nil repeats:YES];
}
showElapsedTime will be called after delay, you provide.
-(void) showElapsedTime: (NSTimer *) timer {
//Reload your webview here
[myWebView reload];
//you also need to invalidate your NSTimer for some condition
if(SomeCondition)
[timer invalidate]
}
Alternatively, I think you could also have the webview reload itself via javascript with the stringByEvaluatingJavaScriptFromString method. Something like this:
[webView stringByEvaluatingJavaScriptFromString:#"setTimeout('location.reload(true);',5000);"];
Not useful in all situations, but maybe in some.