Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

timjver

macrumors newbie
Original poster
Jan 3, 2012
8
0
The Netherlands
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!
 

timjver

macrumors newbie
Original poster
Jan 3, 2012
8
0
The Netherlands
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.

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.
 
Last edited:

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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];
    }
}
 

timjver

macrumors newbie
Original poster
Jan 3, 2012
8
0
The Netherlands
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.

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.

The "trick" is to pass self as the receiver and the sender.

Exactly what I was looking for, thanks!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.