04-23-2010, 01:50 PM
#1 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
How do you put a (IBAction) inside an "if" statement???
Here is my code
Code:
if (row == 0) {
- (IBAction)go {
}
}
When I try to run it I get this error,
Code:
error: 'go' undeclared (first use in this function)
error: expected ';' before '{' token
But when I move the IBAction code outside of the if statement everything builds fine. What am I doing wrong?!!?!?!?
Thanks in advanced!
04-23-2010, 01:58 PM
#2 (permalink )
Registered Member
Join Date: May 2009
Posts: 211
Quote:
Originally Posted by
tackey123
Here is my code
Code:
if (row == 0) {
- (IBAction)go {
}
}
When I try to run it I get this error,
Code:
error: 'go' undeclared (first use in this function)
error: expected ';' before '{' token
But when I move the IBAction code outside of the if statement everything builds fine. What am I doing wrong?!!?!?!?
Thanks in advanced!
I think you are misunderstanding how this works. Declare your method somewhere in your file, and then do something like this:
Code:
-(IBAction)myIBActionMethod
{
//Do something here
}
-(void)didSelectRowAtIndexPath: ...
{
if(row == 0)
{
[self myIBActionMethod];
}
}
04-23-2010, 02:06 PM
#3 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
Quote:
Originally Posted by
csnplt
I think you are misunderstanding how this works. Declare your method somewhere in your file, and then do something like this:
Code:
-(IBAction)myIBActionMethod
{
//Do something here
}
-(void)didSelectRowAtIndexPath: ...
{
if(row == 0)
{
[self myIBActionMethod];
}
}
No, what I am trying to do is make it so the button doesn't do anything when you click on it until the "if" statement is true.
04-23-2010, 02:17 PM
#4 (permalink )
Registered Member
Join Date: May 2009
Posts: 211
Quote:
Originally Posted by
tackey123
No, what I am trying to do is make it so the button doesn't do anything when you click on it until the "if" statement is true.
I see. Do something like this:
Code:
-(IBAction)myIBActionMethod
{
//Do something here
}
if(row == 0)
{
[self myIBActionMethod];
}
04-23-2010, 02:29 PM
#5 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
Quote:
Originally Posted by
csnplt
I see. Do something like this:
Code:
-(IBAction)myIBActionMethod
{
//Do something here
}
if(row == 0)
{
[self myIBActionMethod];
}
No still not right.
What I am trying to do is something similar to the app iFart, it has a scroll wheel and you choose what sound you want them press a button and it plays.
Here is where the problem is:
Code:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (row == 1) {
- (IBAction)go {
//do someting
}
}
}
04-23-2010, 02:32 PM
#6 (permalink )
So cunning its ridiculous
Join Date: Apr 2009
Age: 17
Posts: 135
why don't you put inside the IBAction an if statement seeing if it is the correct row and if it isn't just don't do anything
04-23-2010, 02:40 PM
#7 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
Quote:
Originally Posted by
CunningCat
why don't you put inside the IBAction an if statement seeing if it is the correct row and if it isn't just don't do anything
I tried that:
Code:
- (IBAction)fart {
if (row == 1) {
}
}
But I get this error:
Code:
error: 'row' undeclared (first use in this functions)
04-23-2010, 02:42 PM
#8 (permalink )
almostfunnydev
iPhone Dev SDK Supporter
Join Date: Oct 2009
Age: 34
Posts: 3,015
I think this is the code you want:
Code:
- (void)go1 {
//do someting
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (row == 1) {
[self go1];
}
}
HTH
04-23-2010, 02:48 PM
#9 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
Quote:
Originally Posted by
rocotilos
I think this is the code you want:
Code:
- (void)go1 {
//do someting
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (row == 1) {
[self go1];
}
}
HTH
Yes that works, but I can't put an - (IBAction) inside a - (void). I get errors saying undeclared. I guess the question is really, "how do you put a (IBAction) inside a (void)"
04-23-2010, 02:56 PM
#10 (permalink )
almostfunnydev
iPhone Dev SDK Supporter
Join Date: Oct 2009
Age: 34
Posts: 3,015
nothing
Last edited by rocotilos; 04-23-2010 at 03:01 PM .
04-23-2010, 03:01 PM
#11 (permalink )
almostfunnydev
iPhone Dev SDK Supporter
Join Date: Oct 2009
Age: 34
Posts: 3,015
Quote:
What I am trying to do is something similar to the app iFart, it has a scroll wheel and you choose what sound you want them press a button and it plays.
I didn't notice the above sentence.
So basically you need to declare a global variable of int.
Then you'd assign whatSound to the value of row. and call the play method based on value of whatSound.
Code:
- (IBAction)go1 {
switch (whatSound) {
case 0: // do nothing
break;
case 1: // play sound 1
break;
case 2: // play sound 2
break;
default: // do nothing
break;
}
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
whatSound = row +1;
}
go1 is IBAction connected to the button to play the sound.
HTH
Last edited by rocotilos; 04-23-2010 at 03:04 PM .
04-23-2010, 03:01 PM
#12 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
Quote:
Originally Posted by
rocotilos
Why do want to put an IBAction inside the void?
It does not work that way.
What event is the IBAction connected to?
The way you have it set up is you select something from the list and it plays a sound, what I want is you have to select a sound from the list then press a button in order for it to play.
The IBAction is connected to a button.
04-23-2010, 03:05 PM
#13 (permalink )
almostfunnydev
iPhone Dev SDK Supporter
Join Date: Oct 2009
Age: 34
Posts: 3,015
The code u want is given above your last post.
04-23-2010, 03:06 PM
#14 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 3
Quote:
Originally Posted by
tackey123
Yes that works, but I can't put an - (IBAction) inside a - (void). I get errors saying undeclared. I guess the question is really, "how do you put a (IBAction) inside a (void)"
You can't define a function in serial code. If you're having trouble with that concept, you would benefit more from studying the basics than from trying to force your way through an IPhone application. Even if it's mostly interface builder, it's gonna give you hell - try picking up one of the recommended books (listed on a stick in the iphone sdk development forum).
Good luck.
04-23-2010, 03:10 PM
#15 (permalink )
Registered Member
Join Date: Sep 2009
Age: 15
Posts: 93
Quote:
Originally Posted by
rocotilos
I didn't notice the above sentence.
So basically you need to declare a global variable of int.
Then you'd assign whatSound to the value of row. and call the play method based on value of whatSound.
Code:
- (IBAction)go1 {
switch (whatSound) {
case 0: // do nothing
break;
case 1: // play sound 1
break;
case 2: // play sound 2
break;
default: // do nothing
break;
}
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
whatSound = row +1;
}
go1 is IBAction connected to the button to play the sound.
HTH
YESSSSSS!!!! thats it thanks!
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: 310
20 members and 290 guests
baja_yu , cgokey , Domele , Duncan C , Fstuff , gbenna , givensur , guusleijsten , HowEver , iphonedevshani , jbro , JoeRCruso , mdpauley , n00b , newDev , seokwon lee , SLIC , stanny , Steven.C , WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,875
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031