tags:

views:

65

answers:

2

Im a newbie to Objective -C and having some issues with an assignment. The question is: Add a Method declaration called buttonClick that takes in a variable of type id called sender, and "returns" an IBAction event

I have no idea how to do this This is what I have so Far but getting errors

// method declaration called ButtonClick
@property (nonatomic,assign)  id  ButtonClick;
- (IBAction)return:(id)sender;
@end
A: 

Trying to make a button press method? Google is your friend. But this may help:

- (IBAction)ButtonClick:(id)sender {

    [self insertOtherMethodToDoHere];

}

Not sure what you mean by returns an IBAction, but hope that helped.

XenElement
Don't name your methods using an initial uppercase letter. The convention for method naming is [lowerCamelCase](http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms).
dreamlax
I was using his already intended method name. See the question. But you are correct, that's the better way to do it.
XenElement
+2  A: 

Methods are declared in an @interface and defined in an @implementation. An interface declaration is usually put in a .h file and looks something like this:

// Here we are deriving from NSObject, but it is not uncommon to subclass
// from other classes like NSView.

@interface MyClass : NSObject
{
    int clickCount;
}

- (IBAction) buttonClick:(id) sender;
- (IBAction) resetCounter:(id) sender;

@end

The implementation of the method typically goes in a .m file, and can look something like this:

@implementation MyClass

- (IBAction) buttonClick:(id) sender
{
    clickCount++;
    NSLog(@"Button has been clicked %d time(s)", clickCount);
}

- (IBAction) resetCounter:(id) sender
{
    clickCount = 0;
}

@end

Use Interface Builder to connect one button to the buttonClick: method, and another button to the resetCounter: method.

dreamlax
Answered the question, helpful comments and no snark. You're winning at Stack Overflow.
willc2
Thanks, This helped out alot
Kevin
@Kevin: if this is the answer you were looking for, you should accept it.
JeremyP