天天看點

Mac開發_NSPopUpButton

1、基礎建立

  • 代碼
// 建立
NSPopUpButton *pop_btn = [[NSPopUpButton alloc] init];
// 位置尺寸
pop_btn.frame = CGRectMake(50, 150, 120, 50);
// 添加
[self.window.contentView addSubview:pop_btn];
// pullsDown 設定為 YES 隻有向下的箭頭
pop_btn.pullsDown = NO;
// 當互動事件發生時,是否禁用選項
pop_btn.autoenablesItems = YES;
// 彈出菜單的位置
pop_btn.preferredEdge = NSRectEdgeMaxX;

// 逐個添加項目
[pop_btn addItemWithTitle:@"城市"];
[pop_btn addItemWithTitle:@"上海"];
[pop_btn addItemWithTitle:@"廣州"];
// 批量添加項目
[pop_btn addItemsWithTitles:@[@"深圳", @"河南", @"桂林"]];

// 添加點選事件
[pop_btn setTarget:self];
[pop_btn setAction:@selector(pop_Tap:)];

// 點選選中事件
- (void)pop_Tap:(NSPopUpButton *)pop_btn {
    pop_btn.title = pop_btn.selectedItem.title;
    
    NSLog(@"NSPopUpButton == %@", pop_btn.title);
}      
  • 效果

2、方法說明

// 初始化方法 flag參數決定是下拉菜單模式還是彈出菜單模式
- (instancetype)initWithFrame:(NSRect)buttonFrame pullsDown:(BOOL)flag;
// 設定下拉菜單
@property (nullable, strong) NSMenu *menu;
// 設定當互動事件發生時,是否禁用選項
@property BOOL autoenablesItems;
// 風格設定是否為下拉菜單
@property BOOL pullsDown;
// 設定菜單彈出的優先位置
@property NSRectEdge preferredEdge;

// 清單按鈕相關
// 添加一個按鈕
- (void)addItemsWithTitles:(NSArray<NSString *> *)itemTitles;
// 插入一個按鈕
- (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index;
// 通過标題移除一個按鈕
- (void)removeItemWithTitle:(NSString *)title;
// 通過索引移除按鈕
- (void)removeItemAtIndex:(NSInteger)index;
// 移除所有按鈕
- (void)removeAllItems;
// 所有清單選項按鈕數組
@property (readonly, copy) NSArray<NSMenuItem *> *itemArray;
// 按鈕個數
@property (readonly) NSInteger numberOfItems;
// 擷取按鈕索引的方法
- (NSInteger)indexOfItem:(NSMenuItem *)item;
- (NSInteger)indexOfItemWithTitle:(NSString *)title;
- (NSInteger)indexOfItemWithTag:(NSInteger)tag;
- (NSInteger)indexOfItemWithRepresentedObject:(nullable id)obj;
- (NSInteger)indexOfItemWithTarget:(nullable id)target andAction:(nullable SEL)actionSelector;
// 擷取按鈕的方法
- (nullable NSMenuItem *)itemAtIndex:(NSInteger)index;
- (nullable NSMenuItem *)itemWithTitle:(NSString *)title;
// 擷取最後一個按鈕
@property (nullable, readonly, strong) NSMenuItem *lastItem;
// 選擇某個按鈕的方法
- (void)selectItem:(nullable NSMenuItem *)item;
- (void)selectItemAtIndex:(NSInteger)index;
- (void)selectItemWithTitle:(NSString *)title;
- (BOOL)selectItemWithTag:(NSInteger)tag;
- (void)setTitle:(NSString *)string;
// 擷取選中的按鈕
@property (nullable, readonly, strong) NSMenuItem *selectedItem;
// 擷取已經選中的按鈕索引
@property (readonly) NSInteger indexOfSelectedItem;
// 擷取已經選中的按鈕tag
@property (readonly) NSInteger selectedTag;
// 将選中的标題顯示進行同步
- (void)synchronizeTitleAndSelectedItem;

// 擷取某個索引按鈕的标題
- (NSString *)itemTitleAtIndex:(NSInteger)index;
// 擷取按鈕标題數組
@property (readonly, copy) NSArray<NSString *> *itemTitles;
// 擷取選中的按鈕标題
@property (nullable, readonly, copy) NSString *titleOfSelectedItem;
// 當下拉菜單彈出時發送的通知
APPKIT_EXTERN NSNotificationName NSPopUpButtonWillPopUpNotification;      

3、自定義子項目

  • 3.1 建立

// 建立
NSPopUpButton *pop_btn = [[NSPopUpButton alloc] init];
// 位置尺寸
pop_btn.frame = CGRectMake(50, 200, 120, 50);
// 添加
[self.window.contentView addSubview:pop_btn];
// pullsDown 設定為 YES 隻有向下的箭頭
pop_btn.pullsDown = NO;
// 當互動事件發生時,是否禁用選項
pop_btn.autoenablesItems = YES;
// 彈出菜單的位置
pop_btn.preferredEdge = NSRectEdgeMaxX;

NSArray *pop_Items = @[@"廣州", @"深圳", @"桂林", @"廣州", @"深圳", @"桂林", @"廣州", @"深圳", @"桂林", @"廣州", @"深圳", @"桂林"];
for (NSInteger index = 0; index < pop_Items.count; index++) {
    GC_MenuItem *menu_Item = [[GC_MenuItem alloc ] init];
    menu_Item.title = pop_Items[index];
    [pop_btn.menu addItem:menu_Item];
    menu_Item.target = self;
    menu_Item.action = @selector(pop_Tap:);
}

// 點選選中事件
- (void)pop_Tap:(NSPopUpButton *)pop_btn {
    pop_btn.title = pop_btn.selectedItem.title;
    
    NSLog(@"NSPopUpButton == %@", pop_btn.title);
}      
  • 3.2 自定義類

    • GC_MenuItem.h
    #import <Cocoa/Cocoa.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface GC_MenuItem : NSMenuItem
    
    @end
    
    NS_ASSUME_NONNULL_END      
    • GC_MenuItem.m
    #import "GC_MenuItem.h"
    
    @interface GC_MenuView : NSView
    
    @end
    
    @implementation GC_MenuView
    
    - (void)touchesBeganWithEvent:(NSEvent *)event {
        [super touchesBeganWithEvent:event];
        
        NSLog(@"1234567890");
        
    }
    
    @end
    
    @implementation GC_MenuItem
    
    - (void)setTitle:(NSString *)title {
        [super setTitle:title];
        
        GC_MenuView *menu_view = [[GC_MenuView alloc] init];
        menu_view.frame = NSMakeRect(0, 0, 200, 30);
        [self setView:menu_view];
        
        menu_view.wantsLayer = YES;
        if ([title isEqualToString:@"深圳"]) {
            menu_view.layer.backgroundColor = [NSColor redColor].CGColor;
        }
        else if ([title isEqualToString:@"廣州"]) {
            menu_view.layer.backgroundColor = [NSColor lightGrayColor].CGColor;
        }
        else {
            menu_view.layer.backgroundColor = [NSColor blueColor].CGColor;
        }
    }
    
    @end      
  • 3.3 效果

    Mac開發_NSPopUpButton

作者: CH520