I'm tying create a welcome label using NSTimer but its showing the through some warnings
like
undeclared selector hidelable and unused variable timer
I have not used the NSTimer before can one pls tell me where im doing wrong and wt is the right method to do it.I need to give a welcome message when app load after few minities it has to disappear
i have tried this one im not able to get pls help me
this is the code i have to used in the view didload
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(hideLabel:) userInfo:nil repeats:NO];
in storyboard i have used ib label which i want to display message
#property (strong, nonatomic) IBOutlet UILabel *wel;
please any one tell wt is proper way to make this one..
you have not declared hideLabel method.hence it gives that warning
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(hideLabel:) userInfo:nil repeats:NO];
-(void)hideLabel:(NSTimer *)timer{
myLabel.hidden=YES;
}
Try this
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(timerCalled) userInfo:nil repeats:YES];
-(void)timerCalled
{
NSLog(#"Timer Called...");
}
According to your code, You didn't use timer anywhere else, and didn't fire repeatedly. Then you can use this code as below..
[self performSelector:#selector(hideLabel:) withObject:yourLabel afterDelay:60];
Importantly, define your target method,
-(void)hideLabel:(UILabel*)label
{
// your code here...
label.text = #"Fired...";
}
If you does not need to use (extra use of) object of NSTimer then you should create NSTimer such like,
[NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:#selector(hideLabel:) userInfo:nil repeats:NO];
And then you need to declare timer's method other wise after active (60 sec.) timer you will be get error.
- (void)hideLabel:(NSTimer *)theTimer
{
// Timer method code;
}
Related
In the .h file I set up the Goodguy, Building1 and 2, the NSTimer, and the falldown method.
IBOutlet UIImageView *Goodguy;
IBOutlet UIImageView *Building1;
IBOutlet UIImageView *Building2;
NSTimer *GoodguyFall;
-(void)falldown;
In the .m file I set up the GoodguyFall NSTimer, and the code for when an image touches another image it will invalidate the NSTimer. How do I set it up so the timer is valid while it isn't touching the "buildings"?
-(void)viewDidLoad{
GoodguyFall = [ NSTimer scheduledTimerWithTimeInterval: 0.03 target:self selector:#selector(falldown) userInfo:nil repeats:YES];
}
-(void)falldown{
Goodguy.center = (CGPointMake(Goodguy.center.x, Goodguy.center.y + 6));
}
if (CGRectIntersectsRect(Goodguy.frame, Building1.frame)){
[GoodguyFall invalidate];
}
if (CGRectIntersectsRect(Goodguy.frame, Building2.frame)){
[GoodguyFall invalidate];
}
}
To get the Goodguy to fall again after not touching a building, you’ll need to schedule another timer. This can be accomplished by
-(void)BuidlingMoving{
if (CGRectIntersectsRect(Goodguy.frame, Building1.frame)){
[GoodguyFall invalidate];
GoodguyFall = nil;
}
else if (CGRectIntersectsRect(Goodguy.frame, Building2.frame)){
[GoodguyFall invalidate];
GoodguyFall = nil;
} else if (GoodguyFall == nil) {
GoodguyFall = [ NSTimer scheduledTimerWithTimeInterval: 0.03 target:self selector:#selector(falldown) userInfo:nil repeats:YES];
}
}
In this solution, I’m handling the case where Goodguy is no longer in contact with a building.
I am showing a count down timer using UILabel and NSTimer -
-(void)a_Method
{
[coolTimeLbl setNeedsDisplay];
coolTime = 5; // it is an int
coolingTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(cooling) userInfo:nil repeats:YES]; // NSTimer
}
-(void)cooling
{
if (coolTime>0)
{
coolTime = coolTime-1;
NSLog(#" coolTime----%#",coolTime);
coolTimeLbl.text =[NSString stringWithFormat:#"%d",coolTime];
NSLog(#" coolTimeLbl----%#",coolTimeLbl.text);
}
else
{
[coolingTimer invalidate];
coolingTimer = nil;
}
}
The first time everything works fine and I am getting coolTimeLbl.text as - 4 3 2 1 0
But the second time when I call aMethod, coolTimeLbl is not getting updated properly - it is like 3 2 0 etc (some weird behavior)
However both NSLogs (coolTime & coolTimeLbl) print perfectly all the times and values.
Why does this happen? I tried many ways like NSNotification etc.
Please help me to fix this.
If you're calling a_Method more than once before coolingTimer invalidates itself, the timer will tick more than once.
You should add some boolean like ;
BOOL isTimerValid;
in a_Method,
if(!isTimerValid)
{
isTimerValid = YES;
coolingTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(cooling) userInfo:nil repeats:YES]; // NSTimer
}
in cooling,
else
{
isTimerValid = NO;
....
}
Had the same issue in one of my viewControllers and another one was working OK with same NSTimer code. Looked at about 20 SO threads to get it solved. No luck. In my case
myLabel.opaque = false
solved it.
Don't ask me why.
I have one view controller (let's call it ViewController) with timer within its code, and have another view controller (let's call it ContentViewController) with webView within it which shows content depending on which button is clicked.
I have buttons on ViewController which, when clicked, pushes ContentViewController which loads a html file into its webView.
Timers are used to close ContentViewController if it is pushed.
Here's how I create timers:
-(void)createTimer :(NSNumber*)index
{
if (_show)
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:index, #"parameter1", nil];
switch ([index intValue])
{
case 1001:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
case 1002:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
case 1003:
[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
case 1004:
[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
}
}
}
Here's the code in ViewController which closes ContentViewController when timer fired:
-(void)updateUi :(NSTimer *)timer
{
int index = [[timer.userInfo objectForKey:#"parameter1"] intValue];
if([self.navigationController.visibleViewController isKindOfClass:[ContentViewController class]])
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:#"CurrentString"] intValue]==index )
{
[self.navigationController popViewControllerAnimated:YES];
}
}
}
And on web page which is shown on ContentViewController there's a button,when user clicks it ContentView must go back to ViewController. Here's how I do this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"coffee-drink"])
{
NSString *urlString = [[request URL] absoluteString];
NSArray *urlParts = [urlString componentsSeparatedByString:#":"];
if (urlParts.count > 1) ///------------поменял
{
[self.navigationController popViewControllerAnimated:YES];
}
}
return YES;
}
All of above works fine but sometimes it crashes with the following error:
[ContentViewController respondsToSelector:]: message sent to deallocated instance
And also, I have other errors like these:
nested push animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
and
delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds
The last one appears very rarely.
I don't know what am I doing wrong.Can someone help me)?If needed any more information I will provide it!Thanks in advance!
Issue
You are popping your view without leaving ARC to deallocate all objects and delegates, by keeping them alive.
My Solution
For the NSTimer
At #implementation create NSTimer *timer and use it when you want to initialize it. To dealloc it correctly when you pop back, at viewWillDisappear set [timer invalidate] and timer = nil.
For delegates
Set you specific delegates to nil. For example self.delegate = nil
#Yuandra Ismiraldi is right. Your NSTimer will call your updateUI method every 20/10 seconds repeatedly untill it receives [timer invalidate]; instruction. You are getting this error messages as you navigate through your view controller tree. Sometimes your NavigationViewController will need more memory and will release some of un-used objects. In your case some of the previously shown view controllers that still runs the updateUI method every 20/10 seconds. Once this view controller has been released and your selector is called you will receive this crash.
I hope this explains why sometimes you get this crash, and sometimes not.
Your timer code
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES]
the repeat parameter is set to YES, this make your timer always repeat, thus repeating the sending of the message updateUI after the timer has run for the first time and the view has popped, thus resulting in your error. To make sure your timer only run once, set the repeat to NO
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:NO]
I'm making a flashlight app and i'm in problem
how to blink the flashlight with uiswitch, uislder and nstimer.
i found something on web but I can't understand it.
I already done how to turn on the flashlight(LED)
but I can't find how to blink it.
Also, I can't use NSTimer well.
self.blinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(timerUpdate) userInfo:nil repeats:YES];
I found this code in Stack Overflow but #selector(timerUpdate) and scheduledTimerWithTimeInterval:0.3 is problem.
my plans are
uislider's value --> scheduledTimerWithTimeInterval:(uislder's value)
uiswitch --> start blinking the flashlight.
- (void)toggleFlashlight
{
}
- (IBAction)blink:(UISwitch *)sender {
}
- (IBAction)blinkspeed:(UISlider *)sender {
int progress = lroundf(sender.value);
self.blinksliderlabel.text = [NSString stringWithFormat:#"%dms", progress];
}
Sadly, I don't know.. how to insert the code there...
Please help :(
I like to set the interval of a timer with an UISlider. I already have the value and the value as string, now I have to assign the value to the timer. Can someone please help me?
slidervaluefortimer = [NSString stringWithFormat:#"%f",slider.value];
You just use the slider.value as the argument for this method:
NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:slider.value target:self selector:#selector(myMethod:) userInfo:nil repeats:NO];