Kevin
Kevin

Reputation: 1

Objective-C Question about adding a method

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

Upvotes: 0

Views: 187

Answers (2)

dreamlax
dreamlax

Reputation: 95335

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.

Upvotes: 2

Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

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.

Upvotes: 0

Related Questions