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 02-19-2009, 04:11 PM   #1 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Arrow passing selector as a variable

Hi,

I have a timer that has to fire 3 different methods: m1, m2 and m3.

I have a string called methodName that contains the name of the method that I have to fire.

What I am trying is this...

Code:
	NSTimer *timerStart = [[NSTimer scheduledTimerWithTimeInterval:0.5
	target:self
	selector:@selector( methodName )
	userInfo:nil	repeats:NO]
	retain];

	[timerStart release];
I need to pass the method name inside the timer's selector. Is that possible?

thanks for any help
LunarMoon is offline   Reply With Quote
Old 02-19-2009, 04:15 PM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
Default

SEL yourVariable = @selector(yourSelector:);
SkylarEC is offline   Reply With Quote
Old 02-19-2009, 04:28 PM   #3 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default

Quote:
Originally Posted by SkylarEC View Post
SEL yourVariable = @selector(yourSelector;
Thanks, but I am not sure if I understood that...

My string variable is formed like this...

Code:
// according to x value we will have m1, m2 or m3 as string.
NSString * methodName = [NSString stringWithFormat:@"m%@", x ];

SEL myMethod = @selector(methodName);

// then what?

NSTimer *myTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
	  target:self		selector:myMethod
	userInfo:nil	repeats:NO]		retain];
	[myTimer release];
???? this do not seems to work.
I am receiving an error message telling me that methodName variable is not being used...
LunarMoon is offline   Reply With Quote
Old 02-19-2009, 04:41 PM   #4 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

You can't pass a variable to @selector - you can only pass the actual name of a method.

Try the 'NSSelectorFromString' function instead.
RickMaddy is offline   Reply With Quote
Old 02-19-2009, 04:44 PM   #5 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by LunarMoon View Post
Thanks, but I am not sure if I understood that...

My string variable is formed like this...

Code:
// according to x value we will have m1, m2 or m3 as string.
NSString * methodName = [NSString stringWithFormat:@"m%@", x ];

SEL myMethod = @selector(methodName);

// then what?

NSTimer *myTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
	  target:self		selector:myMethod
	userInfo:nil	repeats:NO]		retain];
	[myTimer release];
???? this do not seems to work.
I am receiving an error message telling me that methodName variable is not being used...
I suspect that the "@selector(methodName)" in your code isn't doing what you think it's doing.

My understanding of the @selector(some_name_here) construct is that it's an Objective-C directive that instructs the compiler to build a selector with the named parameter. So "@selector(methodName)" builds a selector with the name "methodName", and that's it. The "methodName" used to build the selector has no association with the "methodName" variable you have declared in your code.

Is the "x" that you're using to determine your callback a persistent variable? If so, there is a perhaps a simple solution to this problem. Can you see it?
Kalimba is offline   Reply With Quote
Old 02-19-2009, 04:52 PM   #6 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default

ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhh!!!!!!!!!!!!

thank you guys!!! you're the best!
LunarMoon is offline   Reply With Quote
Old 02-19-2009, 04:58 PM   #7 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
Default

RickMaddy, I have had absolutely no problem on the iPhone setting the SEL variable and using it. That's why the SEL variable exists, after all.

Code:
//Create your selectors
SEL myMethod = @selector(aMethod:);
//etc...

/*____________________________The rest of your code.____________________________*/

//
aTimer = [NSTimer scheduledTimerWithTimeInterval: 0.75 target:self selector:myMethod userInfo:nil repeats:YES];
As you see, just set it and use it as you would any other variable.
SkylarEC is offline   Reply With Quote
Old 02-19-2009, 05:17 PM   #8 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Sorry, you misunderstood my post. What you can't do is pass a variable to the @selector directive. Of course you can assign the result of @selector to a SEL variable. The part in the parentheses can't be a variable.
RickMaddy is offline   Reply With Quote
Old 02-19-2009, 05:18 PM   #9 (permalink)
Registered Member
 
tkilmer's Avatar
 
Join Date: May 2008
Posts: 583
Default

Lunar was originally trying to set the selector using an NSString. What you are doing is right, but if you want to set the selector based off of a string like Lunar originally attempted, Ricks solution is correct.
tkilmer is offline   Reply With Quote
Old 02-19-2009, 05:30 PM   #10 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 74
Default

Alright, I did misunderstand. My apologies.
SkylarEC is offline   Reply With Quote
Old 02-19-2009, 05:38 PM   #11 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

Quote:
Originally Posted by SkylarEC View Post
Alright, I did misunderstand. My apologies.
No problem. Just don't let it happen again.
RickMaddy is offline   Reply With Quote
Old 02-19-2009, 06:01 PM   #12 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 280
Default

thank you all.
it is now working perfectly!!!
LunarMoon is offline   Reply With Quote
Reply

Bookmarks

Tags
selector, string

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: 253
18 members and 235 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, F_Bryant, GHuebner, HDshot, headkaze, mer10, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
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 12:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0