12-28-2009, 08:42 PM
#1 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 171
NSTimer Trouble
Hello All,
I am having a bit of trouble with a Timer that should call a method every second. I have the timer set up here
//create a UIButton and send it to moveTimer, which should activate the UITimer and move the button every 1 second.
Code:
-(void)moveTimer:(UIButton *)button{
moveTime = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(moveBalls:) userInfo:button repeats:YES];
and this is the method it should be calling every second.
Code:
-(void)moveBalls:(UIButton *)ball{
ball.frame = CGRectMake(ball.frame.origin.x, ball.frame.origin.y + 1, 20, 20);
}
It doesn't seem to be working. Any ideas as to why?
12-28-2009, 09:53 PM
#2 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Define "not working".
Looking quickly, it looks to me like it should be crashing when the timer fires. Is that what is happening?
12-28-2009, 10:12 PM
#3 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 171
Quote:
Originally Posted by
eddietr
Define "not working".
Looking quickly, it looks to me like it should be crashing when the timer fires. Is that what is happening?
Yes. The app crashes when its fired. How come?
12-28-2009, 10:23 PM
#4 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by
dalson
Yes. The app crashes when its fired. How come?
Because the timer expects your method to be of the form:
Code:
- (void) doSomething:(NSTimer*) timer {
// react to the timer
}
The first argument passed to your method is the timer itself.
Your method is this:
Code:
-(void)moveBalls:(UIButton *)ball{
ball.frame = CGRectMake(ball.frame.origin.x, ball.frame.origin.y + 1, 20, 20);
}
So where you think you're getting a UIButton* as your argument, you're really getting an NSTimer*.
and then you go and try to get ball.frame. Well, ball is really an NSTimer* now, not a UIButton*. And NSTimer's don't have a frame.
So you're probably crashing with a "unrecognized selector" error. The selector being 'frame' which is being sent to an instance of NSTimer.
Hope that makes sense?
12-28-2009, 10:37 PM
#5 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 171
Yes, that makes complete sense. Thank you. Is it possible to add a second parameter to the timer so it takes a Timer and a Button in?
12-28-2009, 10:49 PM
#6 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by
dalson
Yes, that makes complete sense. Thank you. Is it possible to add a second parameter to the timer so it takes a Timer and a Button in?
You can specify any method signature you want and any arguments you want if you use an NSInvocation. And then use +[NSTimer scheduledTimerWithTimeInterval:invocation:repeats]
But an easier way is to use the userInfo parameter. NeXT tell you to put a NSDictionary* in there. And then you can access that dict from your method.
I see you just passed the button (UIButton*) in userInfo directly. That should work, too.
Try this:
Code:
-(void)moveBalls:(NSTimer*) timer{
UIButton* ball = (UIButton*) [timer userInfo];
ball.frame = CGRectMake(ball.frame.origin.x, ball.frame.origin.y + 1, 20, 20);
}
See if that works.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 254
24 members and 230 guests
@sandris , ADY , Dani77 , diyora , FAED , fredidf , F_Bryant , GHuebner , HDshot , iDifferent , JasonR , mer10 , Oral B , prchn4christ , Rudy , smithdale87 , Speed , spiderguy84 , stekki , tgjorgoski , Thompson22 , Touchmint , twerner , vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,753
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris