创建视图和项目数组+编辑它们

时间:2016-03-07 12:09:24

标签: xcode swift macos

我正在尝试使用Swift在OSX上创建状态栏应用程序。我想以编程方式为连接到我网站的每个人创建一个视图。

在这些视图中,我想要一个:

  • 图像
  • 标签
  • 进度条

我希望这些项目随时可以编辑。

这是我的实际代码:

var items: [AnyObject] = []
var views: [AnyObject] = []
var images: [AnyObject] = []
var texts: [AnyObject] = []
var bars: [AnyObject] = []

for var i = 0; i < 5; i++ {
        var item : NSMenuItem = NSMenuItem(title: "item\(i)", action: Selector("nil"), keyEquivalent: "")
        statusItem.menu!.addItem(NSMenuItem.separatorItem())
        statusItem.menu!.addItem(item)
        statusItem.menu!.addItem(NSMenuItem.separatorItem())
        items.append(item)

        let view = NSView(frame: CGRect(x: 0, y: 0, width: 265, height: 71))
        item = Menu.itemWithTitle("item\(i)")!
        item.view = view
        views.append(view)

        var image: NSImageView
        image = NSImageView(frame: CGRect(x: 20, y: 9, width: 50, height: 50))
        image.image = NSImage(named: "MacbookAir")
        view.addSubview(image)
        images.append(image)

        var text: NSTextField
        text = NSTextField(frame: CGRect(x: 96, y: 42, width: 158, height: 17))
        text.stringValue = "MacBook Air"
        text.editable = false
        text.bordered = false
        text.backgroundColor = NSColor(white: 1, alpha: 0)
        view.addSubview(text)
        texts.append(text)

        var bar: NSProgressIndicator
        bar = NSProgressIndicator(frame: CGRect(x: 82, y: 7, width: 163, height: 20))
        bar.indeterminate = false
        bar.incrementBy(20.0)
        view.addSubview(bar)
        bars.append(bar)
    }

}

我知道我这样做是错误的,因为在我的脑海中我想做的事情如下:

var bar[i]: NSProgressIndicator
        bar[i] = NSProgressIndicator(frame: CGRect(x: 82, y: 7, width: 163, height: 20))
        bar[i].indeterminate = false
        bar[i].incrementBy(20.0)
        view[i].addSubview(bar[i])
        bars.append(bar[i])

然后就可以编辑:

bar[0].incrementBy(50.0) //incrementing the bar with the array index

这绝对不起作用...... 非常感谢任何帮助。

编辑(添加更多信息):

当我打印所有数组时,我得到了所有这些代码:

   /////Printing Items :
[<NSMenuItem: 0x6000000a1f20 item0>, <NSMenuItem: 0x6000000a2100 item1>, <NSMenuItem: 0x6000000a2340 item2>, <NSMenuItem: 0x6000000a2580 item3>, <NSMenuItem: 0x6000000a27c0 item4>]
/////Printing Views :
[<NSView: 0x600000120820>, <NSView: 0x6000001208c0>, <NSView: 0x600000120960>, <NSView: 0x600000120a00>, <NSView: 0x600000120aa0>]
/////Printing Texts :
[<NSTextField: 0x6080001e0d00>, <NSTextField: 0x6000001e0400>, <NSTextField: 0x6000001e0700>, <NSTextField: 0x6000001e0900>, <NSTextField: 0x6000001e0b00>]
/////Printing images :
[<NSImageView: 0x600000161f80>, <NSImageView: 0x600000161ec0>, <NSImageView: 0x600000162040>, <NSImageView: 0x600000162100>, <NSImageView: 0x6000001621c0>]
/////Printing Bars :
[<NSProgressIndicator: 0x6080001e0e00>, <NSProgressIndicator: 0x6000001e0600>, <NSProgressIndicator: 0x6000001e0800>, <NSProgressIndicator: 0x6000001e0a00>, <NSProgressIndicator: 0x6000001e0c00>]

有没有办法修改条形图,文本等......?

1 个答案:

答案 0 :(得分:0)

您是否正在考虑线程化?

由于NSStatusbar是UI的一部分,它可能在主线程上被触发。所以如果有的话,我会通过以下方式对它进行任何更新:

//Swift
dispatch_async(dispatch_get_main_queue()){
    //do what you have to do here
}