PDA

View Full Version : Triggering a method within a method




timjver
Jan 3, 2012, 12:48 PM
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!



mduser63
Jan 3, 2012, 12:51 PM
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.

timjver
Jan 3, 2012, 01:10 PM
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.

-(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:
{
[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.

admanimal
Jan 3, 2012, 01:50 PM
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
Jan 3, 2012, 01:56 PM
The "trick" is to pass self as the receiver and the sender.


-(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
Jan 3, 2012, 02:28 PM
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!