oc之mac中NSPopUpButton

2016.07.28 16:13* 字数 191 阅读 1662评论 2喜欢 4

NSPopUpButton 下拉列表按钮,有两种。一种只有下拉箭头的按钮,另一种是既有上拉,也有下拉。

  • 如果设置只有向下的箭头,这最上面的item 只会出现一次,一旦其他的item选中之后, 就找不到第一个item了。 所以, 一般初始的item是一个不用的提示,比如要选择地区,那么第一个就放城市的提示信息,就行了。
  • 如果既有上拉按钮也有下拉按钮,那么所有的item都会显示在下拉列表框中。

## ****下面以只有下拉箭头的按钮为例进行说明:

// pullsDown 设置为 YES 只有向下的箭头

    NSPopUpButton *popBtn = [[NSPopUpButton alloc] initWithFrame:CGRectMake(0, 100, 100, 30) pullsDown:YES];

    [popBtn addItemWithTitle:@"城市"];

    [popBtn addItemWithTitle:@"上海"];

    [popBtn addItemWithTitle:@"广州"];

    [popBtn addItemWithTitle:@"深圳"];

    [popBtn addItemWithTitle:@"河南"];

    

    [self.view addSubview:popBtn];

    

    // popBtn 的点击事件

    [popBtn setTarget:self];

    [popBtn setAction:@selector(handlePopBtn:)];

 

NSPopUpButton 选中item的相应事件:

- (void)handlePopBtn:(NSPopUpButton *)popBtn {

    // 选中item 的索引

    NSLog(@"%d", popBtn.indexOfSelectedItem);

//    [popBtn selectItemAtIndex:popBtn.indexOfSelectedItem];

    popBtn.title = popBtn.selectedItem.title;

}

 
  oc之mac中NSPopUpButton 只有下拉按钮.jgp   oc之mac中NSPopUpButton 上拉和下拉按钮都有的效果图

小礼物走一走,来简书关注我

 

NSPopUpButton创建菜单与子菜单

 NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame: 
      NSMakeRect(0, 0, 150, 22)];
      id item;
      NSMenuItem *menuItem;
      NSMenu *submenu;


      [popup setAutoenablesItems: NO];
    
      [popup addItemWithTitle: @"Marguerite"];
      [[popup itemWithTitle: @"Marguerite"] setEnabled: YES];
      [popup addItemWithTitle: @"Julie"];
      [[popup itemWithTitle: @"Julie"] setEnabled: YES];
      [popup addItemWithTitle: @"Liv"];
      [[popup itemWithTitle: @"Liv"] setEnabled: YES];
      [popup addItemWithTitle: @"Juliette"];
      [[popup itemWithTitle: @"Juliette"] setEnabled: YES];

      item = [popup itemWithTitle: @"Julies"];
      [popup selectItem: item];


      [toolbarItem setLabel: @"Just... popup"];
      [toolbarItem setView: popup];
    
      menuItem = [[NSMenuItem alloc] initWithTitle: @"More..." 
                                            action: NULL
                                     keyEquivalent: @""];
      submenu = [[NSMenu alloc] initWithTitle: @""];
      [submenu addItemWithTitle: @"Marguerite" 
                         action: @selector(reflectMenuSelection:) 
                  keyEquivalent: @""];
      [submenu addItemWithTitle: @"Julie" 
                         action: @selector(reflectMenuSelection:) 
                  keyEquivalent: @""];
      [submenu addItemWithTitle: @"Liv" 
                         action: @selector(reflectMenuSelection:) 
                  keyEquivalent: @""];
      [submenu addItemWithTitle: @"Juliette" 
                         action: @selector(reflectMenuSelection:) 
                  keyEquivalent: @""];
      [menuItem setSubmenu: AUTORELEASE(submenu)];
      [toolbarItem setMenuFormRepresentation: AUTORELEASE(menuItem)];

 

 

[Cocoa]_[初级]_[NSPopUpButton重绘实例]

2015年04月28日 19:37:57 Foreveroriginal 阅读数:712

 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moqj_123/article/details/45341071

场景:比如说,定义一个支持多种格式的控件,包含下拉菜单进行多种选择.由于标准的控件在界面上布局和自己所需的相差很多,所以要对控件进行重绘。

一下是具体实例:

类似于带下拉菜单的文本编辑框。

//  MqjPopUpButton.m

//  MqjEditContact

//

//  Created by mac-d on 4/28/15.

//  Copyright (c) 2015 mac-d. All rights reserved.

//

#import "MqjPopUpButton.h"

@implementation MqjPopUpButton

- (id)initWithFrame:(NSRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code here.

    }

    return self;

}

-(BOOL) isFlipped

{

    return YES;

}

- (void)drawRect:(NSRect)dirtyRect

{

    // Drawing code here.

    //[super drawRect:dirtyRect];

    [[NSColor colorWithCalibratedRed: 255/255.0 green:255/255.0 blue:255/255.0 alpha:1] setFill];

    NSRectFill(dirtyRect);

    NSString *path =[[NSBundle mainBundle] pathForResource:@"down" ofType:@"png"];

    NSImage *image =[[NSImage alloc] initWithContentsOfFile:path];

    NSRect rect =NSZeroRect;

    rect.size = [image size];

    NSPoint p = dirtyRect.origin;

    p.x +=dirtyRect.size.width-image.size.width-4;

    p.y +=(dirtyRect.size.height-image.size.height)/2;

    [image drawInRect:NSMakeRect(p.x, p.y, image.size.width, image.size.height) fromRect:rect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];

    //画横线

    NSBezierPath *line = [NSBezierPath bezierPath];

    [line setLineWidth:1];

    [[NSColor colorWithCalibratedRed: 147/255.0 green:147/255.0 blue:147/255.0 alpha:0.5] setStroke];

    NSPoint endPoint = dirtyRect.origin;

    endPoint.x += dirtyRect.size.width;

    [line moveToPoint:dirtyRect.origin];

    [line lineToPoint:endPoint];

    [line stroke];

    [line moveToPoint:NSMakePoint(dirtyRect.origin.x, dirtyRect.origin.y+dirtyRect.size.height)];

    [line lineToPoint:NSMakePoint(endPoint.x,dirtyRect.origin.y+dirtyRect.size.height)];

    [line stroke];

    //画竖线

    [line moveToPoint:dirtyRect.origin];

    [line lineToPoint:NSMakePoint(dirtyRect.origin.x,dirtyRect.origin.y+dirtyRect.size.height)];

    [line stroke];

    [line moveToPoint:endPoint];

    [line lineToPoint:NSMakePoint(endPoint.x,dirtyRect.origin.y+dirtyRect.size.height)];

    [line stroke];

    //对选中的item设置勾选

    NSArray *array = [super itemArray];

    for(NSMenuItem *item in array)

    {

        if (item == [self selectedItem])

        {

            [item setState:NSOnState];

        }

        else

        {

            [item setState:NSOffState];

        }

    }

    NSString *title =[[super selectedItem] title];

    if (title == nil)

    {

        title = @"";

   }

    NSLog(@"title:%@",title);


    //获取字符串的宽度和高度

    NSSize titleSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[self font] forKey:NSFontAttributeName]];

    CGFloat titleY = dirtyRect.origin.y + (dirtyRect.size.height - titleSize.height)/2;

    NSRect rectTitle = dirtyRect;

    rectTitle.origin = NSMakePoint(10, titleY);

    rectTitle.size.height = titleSize.height;

    [title drawInRect:rectTitle withAttributes:nil];

}


@end

oc之mac中NSPopUpButton

在MainMenu.xib布局一个NSPopUpButton,class:改为如图所示。

运行结果:

oc之mac中NSPopUpButton

 

上一篇:android – 如何从适配器类打开弹出窗口


下一篇:uni-app 自定义一个消息弹窗组件