How do I pass a value to a method in Objective C

Posted by user268124 on Stack Overflow See other posts from Stack Overflow or by user268124
Published on 2010-05-17T22:15:28Z Indexed on 2010/05/17 22:20 UTC
Read the original article Hit count: 182

Filed under:

I'm new to Obj-C and I have run in to a very, very basic mental block that's making me feel a little stupid. I want to pass a variable from one method to another in an purely objective-C way. So far I'm stuck and have resorted to writing it in C. In C it goes as follows;

//detect the change in the segmented switch so we can; change text
- (IBAction)switchChanged:(id)sender
{
NSLog(@"Switch change detected");
//grab the info from the sender
UISegmentedControl *selector = sender;
//grab the selected segment info from selector. This returns a 0 or a 1.
int selectorIndex = [selector selectedSegmentIndex];
changeText (selectorIndex);

}

int changeText (int selectorPosition)
{
NSLog(@"changeText received a value. Selector position is %d", selectorPosition);
//Idea is to receive a value from the segmented controler and change the screen text accordingly

return 0;

}

This works perfectly well, but I want to learn how to do this with objects and messages. How would I rewrite these two methods to do this?

Cheers

Rich

© Stack Overflow or respective owner

Related posts about objective-c