Help~Calling a function from a function

Discussion in 'iPhone/iPad Programming' started by Jeremy1026, Jul 19, 2008.

  1. macrumors 68000

    Joined:
    Nov 3, 2007
    #1
    I want to return to a function after another completes, how do I do this?
     
  2. macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    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
    Code:
    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.
     
  3. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #3
    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...

    Code:
    -(IBAction)run {
         do stuff
         reselect action
         start (IBAction)doSomeThingElse
    }
    
     
  4. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #4
    This will do it.

    Code:
    -(IBAction)theFirstFunction:(id)sender{
    
    [self theSecondFunction:self];
    }
    
    -(IBAction)theSecondFunction:(id)sender{
    //Whatever
    }
    
    
    
     
  5. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #5
    Thank you, thats exactly what I needed.
     
  6. macrumors regular

    Joined:
    Jun 10, 2008
    #6
    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?

    Code:
    -(IBAction)theFirstFunction:(id)sender{
    
    [self theSecondFunction:[B][COLOR="Red"]sender[/COLOR][/B]];
    }
    
    -(IBAction)theSecondFunction:(id)sender{
    //Whatever
    }
    
    
    
     
  7. macrumors regular

    Joined:
    Mar 24, 2008
    #7
    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:

    Code:
    [self theSecondFunction:[B][COLOR="Red"]sender[/COLOR][/B]]
     
  8. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #8
    Yes you are correct :D
     

Share This Page