PDA

View Full Version : Help~Calling a function from a function




Jeremy1026
Jul 19, 2008, 11:09 PM
I want to return to a function after another completes, how do I do this?



admanimal
Jul 19, 2008, 11:19 PM
I want to return to a function after another completes, how do I do this?

If you call one function from inside another, the program returns to the first ("outer") function after the 2nd ("inner") function completes.

So if you have

int outer() {

int x = 0;
inner();
x = x + 1;
return x;
}


The program will run inner() and then go back to outer() and return 1.

Jeremy1026
Jul 25, 2008, 07:20 AM
I think I may have asked the question wrong...or worded it wrong.

I have multiple IBActions, each can be called on its own, but after it is called it effects other IBActions. I want to have an IBAction call on another IBAction after it completes its execution.

Something like...

-(IBAction)run {
do stuff
reselect action
start (IBAction)doSomeThingElse
}

MacDonaldsd
Jul 25, 2008, 08:30 AM
I think I may have asked the question wrong...or worded it wrong.

I have multiple IBActions, each can be called on its own, but after it is called it effects other IBActions. I want to have an IBAction call on another IBAction after it completes its execution.

Something like...

-(IBAction)run {
do stuff
reselect action
start (IBAction)doSomeThingElse
}


This will do it.


-(IBAction)theFirstFunction:(id)sender{

[self theSecondFunction:self];
}

-(IBAction)theSecondFunction:(id)sender{
//Whatever
}

Jeremy1026
Jul 25, 2008, 10:38 AM
Thank you, thats exactly what I needed.

kalimba
Jul 25, 2008, 03:02 PM
This will do it.


-(IBAction)theFirstFunction:(id)sender{

[self theSecondFunction:self];
}

-(IBAction)theSecondFunction:(id)sender{
//Whatever
}




Will that do what the OP wants? Keep in mind that I'm pretty new to Cocoa/Obj-C, but would the following snippet be the proper way to chain to a second function, and if not, why not?


-(IBAction)theFirstFunction:(id)sender{

[self theSecondFunction:sender];
}

-(IBAction)theSecondFunction:(id)sender{
//Whatever
}

SqueegyX
Jul 25, 2008, 03:23 PM
Will that do what the OP wants? Keep in mind that I'm pretty new to Cocoa/Obj-C, but would the following snippet be the proper way to chain to a second function, and if not, why not?


-(IBAction)theFirstFunction:(id)sender{

[self theSecondFunction:sender];
}

-(IBAction)theSecondFunction:(id)sender{
//Whatever
}

I was thinking the same thing. Although the sender argument is rarely used (in my experience at least), and it is of type id so it could be any object (or nil). So you could probably pass whatever you want to it unless your app depends on its value, usually when multiple buttons are linked to the same action and you need to detect which one got hit.

But yeah this is probably the "more right" way to do it:

[self theSecondFunction:sender]

MacDonaldsd
Jul 26, 2008, 06:07 AM
Yes you are correct :D