Triggering a method within a method

Discussion in 'Mac Programming' started by timjver, Jan 3, 2012.

  1. macrumors newbie

    Joined:
    Jan 3, 2012
    Location:
    The Netherlands
    #1
    I'm learning the Objective-C language. In my code, I have three times the same method, I think there must be a way to only have to write it once and redirect to that piece of code for the other two times I need it. Is there a way to trigger a method within another method? Thanks in advance!
     
  2. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #2
    Please post the code you've got. It's hard to tell exactly what you're asking about without being able to see your code.
     
  3. timjver, Jan 3, 2012
    Last edited: Jan 3, 2012

    thread starter macrumors newbie

    Joined:
    Jan 3, 2012
    Location:
    The Netherlands
    #3
    Thanks for your reply.

    Code:
    -(IBAction)useSomething:(id)sender
    {
        [textField setIntValue:[addField intValue]];
        if ([updateChecker state] == 1 && [addFieldTwo intValue] != 0)
        {
            [textFieldTwo setIntValue:[textField intValue]+[addFieldTwo intValue]];
        }
    }
    
    -(IBAction)useOneLess:(id)sender
    {
        [textField setIntValue:[addField intValue] - 1];
        if ([updateChecker state] == 1 && [addFieldTwo intValue] != 0)
        {
            [textFieldTwo setIntValue:[textField intValue]+[addFieldTwo intValue]];
        }
    }
    
    -(IBAction)addSomething:(id)sender
    {
        [textFieldTwo setIntValue:[textField intValue]+[addFieldTwo intValue]];
    }
    Three times, I used the code of the addSomething method:
    Code:
    {
        [textFieldTwo setIntValue:[textField intValue]+[addFieldTwo intValue]];
    }
    Now, I wonder if there is a way to tell the useSomething and useOneLess methods to execute the code of the addSomething method. In this way, when I change the code of the addSomething method, the other methods will be changed as well, without having to change them manually.
     
  4. macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    Each of those blocks of code you have are called methods or functions (i.e. useSomething, useOneLess, addSomething). The main purpose of functions is to be able to reuse code in exactly the way you are hoping for.

    I would recommend looking at any very basic tutorial on Objective-C or even just programming in general, as what you are asking about is a very fundamental concept that you should become familiar with.
     
  5. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    The "trick" is to pass self as the receiver and the sender.

    Code:
    -(IBAction)useSomething:(id)sender
    {
        [textField setIntValue:[addField intValue]];
        if ([updateChecker state] == 1 && [addFieldTwo intValue] != 0)
        {        
            // [textFieldTwo setIntValue:[textField intValue]+[addFieldTwo intValue]];
            [self addSomething:self];
        }
    }
    
     
  6. timjver, Jan 3, 2012
    Last edited: Jan 7, 2012

    thread starter macrumors newbie

    Joined:
    Jan 3, 2012
    Location:
    The Netherlands
    #6
    Thanks. As I said, I'm still learning to code and am going through tutorials and looking for suitable ones. The problem was that I knew how to trigger a method by pressing a button, but not by just some piece of code.

    Exactly what I was looking for, thanks!
     

Share This Page