Xcode_OSX/Swift_NSPopUpButton. [英] Xcode_OSX/Swift_NSPopUpButton.

查看:157
本文介绍了Xcode_OSX/Swift_NSPopUpButton.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此非常陌生,所以请记住这一点!

I am incredibly new to this, so please keep that in mind!

我整晚都在这里,看了无数的视频/出没的地方 无数论坛... 我找不到一个答案!

I've been at this all night, watched countless videos/haunted countless forums...I can't find one single answer!

我正在尝试在Swift/OSX中创建一个基本的弹出菜单,我需要弄清楚的是:

I am trying to make a basic popup menu in Swift/OSX What I need to figure out is:

  • 如何在此菜单中添加超过三个项目"的所有内容
  • 无论在弹出窗口中选择什么,该信息都会发送一个整数 值到另一个数字.
  • How can I add more than the 'three items' to this menu
  • Whatever is selected in the popup, for that info to send an integer value to another number.

非常感谢您的帮助,谢谢.

I very much would appreciate your help, Thanks.

推荐答案

NSPopupButton是一堆NSMenuItem对象的容器,因此添加项可以使用

A NSPopupButton is a container for a bunch of NSMenuItem objects so to add an item you can use

func addItemWithTitle(_ title: String!)

通过调用为您构造NSMenuItem.

并且您可能希望从头开始,可以使用

and as you may wish to start from scratch you can use

func removeAllItems()

要清除按钮上的现有项目.

To clean existing items from the button.

还有其他方法左右移动按钮中的菜单项.

There are also other methods around moving and removing menu items from the button.

A NSPopupButton是-a NSControl,因此您可以使用var action: Selector设置选择项目时发送的操作,并使用var target: AnyObject!控制哪个对象接收消息.或者只是将其连接到Interface Builder.

A NSPopupButton is-a NSControl so you can use var action: Selector to set the action sent when an item is selected and var target: AnyObject! to control which object receives the message. Or just wire it up in Interface Builder.

protocol FooViewDelegate{
    func itemWithIndexWasSelected(value:Int)
}

class FooViewController: NSViewController  {

    @IBOutlet weak var myPopupButton: NSPopUpButton!
    var delegate: FooViewDelegate?

    let allTheThings = ["Mother", "Custard", "Axe", "Cactus"]

    override func viewDidLoad() {
        super.viewDidLoad()
        buildMyButton()
    }

    func buildMyButton() {
        myPopupButton.removeAllItems()

        myPopupButton.addItemsWithTitles(allTheThings)
        myPopupButton.target = self
        myPopupButton.action = "myPopUpButtonWasSelected:"

    }

    @IBAction func myPopUpButtonWasSelected(sender:AnyObject) {

        if let menuItem = sender as? NSMenuItem, mindex = find(allTheThings, menuItem.title) {
            self.delegate?.itemWithIndexWasSelected(mindex)
        }
    }


}

所有按钮的构造都可以在Interface Builder中完成,而不是代码.请记住,您可以使用CMD-D复制项目,也可以将新的NSMenuItem对象拖动到按钮中.

All the button construction can be done in Interface Builder rather than code too. Remember that you can duplicate items with CMD-D or you can drag new NSMenuItem objects into the button.

这篇关于Xcode_OSX/Swift_NSPopUpButton.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆