MAC OS X Cocoa NSToolbar 的使用

原创 2005年03月04日 15:29:00

- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
(NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
     // Required delegate method   Given an item identifier, self method
returns an item
     // The toolbar will use self method to obtain toolbar items that can
be displayed in the customization sheet, or in the toolbar itself
     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier: itemIdent] autorelease];

     if ([itemIdent isEqual: SquareDotViewItemIdentifier]) {
 SquareDotView *dv = [[SquareDotView alloc] initWithFrame:

 [toolbarItem setView: dv];

 // set a reasonable minimum size
 [toolbarItem setMinSize: NSMakeSize(100,32)];
 // set a maximum size that allows us to stretch.
 [toolbarItem setMaxSize: NSMakeSize(300,32)];

 [toolbarItem setLabel: @"Dot View"];
 [toolbarItem setPaletteLabel: @"A Dot View"];
 [toolbarItem setToolTip: @"This is a dot view"];

- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{// return an array of all  the items that can be highlight display after selected
   return [NSArray arrayWithObjects:

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{ // return an array of the items found in the default toolbar
    return [NSArray arrayWithObjects:

The "SquareDotView" class:

@interface SquareDotView : NSView {
     float sdSide;
     NSPoint sdLocation;

@implementation SquareDotView

#define START_SIDE  10
#define START_LOCATION  NSMakePoint(10,10)

- (id)initWithFrame:(NSRect)frame {
     self = [super initWithFrame:frame];
     if (self) {
 sdSide = START_SIDE;
 sdLocation = START_LOCATION;
     return self;

- (void)mouseDown:(NSEvent *)theEvent {
     sdLocation = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
     [self setNeedsDisplay: YES];

- (void)drawRect:(NSRect)rect {
     [[NSColor redColor] set];
     [[NSBezierPath bezierPathWithRect: NSMakeRect(sdLocation.x,
sdLocation.y, sdSide, sdSide)] fill];

     [[NSColor blackColor] set];
     [[NSBezierPath bezierPathWithRect: [self bounds]] stroke];

- (id)initWithCoder:(NSCoder *)coder {
     self = [super initWithCoder: coder];
     if (self) {
 [coder decodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
     return self;

- (void)encodeWithCoder:(NSCoder *)coder {
     [super encodeWithCoder: coder];
     if (self) {
 [coder encodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,




Cocoa Programming For Mac Os X 3rd

  • 2013-11-05 10:13
  • 9.93MB
  • 下载

Cocoa Programming for Mac OS X 第二章(Let's Get Started)摘录

1.Instance variables that are pointers to other objects are called outlets. Methods that can be trig...

Cocoa Programming for Mac OS X 第十三章(User Defaults)摘录

Many applications have Preferences panels that allow the user to choose a preferred appearance or be...

Cocoa Programming for Mac OS X 第十六章(Localization)摘录

If the application you create is useful, you will want to share it with all the people of the world....