1. Create new Cocoa application.
2. Add new Objective-C class (DoButton). By default, the class is derived from NSObject. Change it - NSButtonCell.
3. Modify the class interface and implementation as it is shown below.
DoButton.h file:
@interface DoButton : NSButtonCell {
}
- (IBAction)DoSomething:(id)sender;
@end
DoButton.m file:
#import "DoButton.h"
@implementation DoButton
- (IBAction)DoSomething:(id)sender
{
NSLog(@"Hello");
}
@end
3. Save the project file and switch to Interface Builder (double-click on the MainMenu.xib file).
4. In Interface Builder, add Round Rect Button to the application window.
5. In the Library find your class (DoButton), choose it by the mouse click and bring to the MainMenu.xib document.
6. Select the Round Rect Button in the window. In the Inspector, on the Class Identity tab, choose DoButton from the combo box as the class name.
7. Select the Round Rect Button in the window and connect it with DoButton object in the MainMenu.xib document:
8. Save changes in the Interface Builder and switch back to the application code.
9. Build and Debug the application.
In the console you see:
Running…
2010-04-23 22:52:57.626 ButtonApp[21939:a0f] Hello
2010-04-23 22:52:59.387 ButtonApp[21939:a0f] Hello
Same can be done with NSMenu. In this case the first Objective-C class should derive from NSMenu. All other things are absolutely the same.
No comments:
Post a Comment