Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-28-2009, 08:42 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 171
Default 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?
dalson is offline   Reply With Quote
Old 12-28-2009, 09:53 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

Define "not working".

Looking quickly, it looks to me like it should be crashing when the timer fires. Is that what is happening?
eddietr is offline   Reply With Quote
Old 12-28-2009, 10:12 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 171
Default

Quote:
Originally Posted by eddietr View Post
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?
dalson is offline   Reply With Quote
Old 12-28-2009, 10:23 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

Quote:
Originally Posted by dalson View Post
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?
eddietr is offline   Reply With Quote
Old 12-28-2009, 10:37 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 171
Default

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?
dalson is offline   Reply With Quote
Old 12-28-2009, 10:49 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
Default

Quote:
Originally Posted by dalson View Post
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.
eddietr is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0