[英]Creating a array of views and items + editing them
I'm trying to make a status bar app on OSX with Swift.我正在尝试使用 Swift 在 OSX 上制作一个状态栏应用程序。 I want to programmatically create a view for each people connected to my website.
我想以编程方式为连接到我网站的每个人创建一个视图。
In these views I want a :在这些视图中,我想要一个:
I want these items to be editable anytime.我希望这些项目可以随时编辑。
This is my actual code :这是我的实际代码:
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)
}
}
I know I'm doing it the wrong way because in my head I wanted to do something like :我知道我做错了,因为在我的脑海里我想做一些事情:
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])
Then it would be editable like that :然后它可以像这样编辑:
bar[0].incrementBy(50.0) //incrementing the bar with the array index
That is definitely not working at all... Any help is greatly appreciated.这绝对是行不通的......非常感谢任何帮助。
EDIT (adding more infos) :编辑(添加更多信息):
When I print all my arrays I'm getting all of this code :当我打印所有数组时,我得到了所有这些代码:
/////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>]
Is there a way to modify bars, texts etc... ?有没有办法修改条形、文本等...?
Are you taking into consideration threading?你在考虑线程吗?
As NSStatusbar is part of the UI it's probably being fired on the main-thread.由于 NSStatusbar 是 UI 的一部分,它可能在主线程上被触发。 So if anything, i'd perform any updates to it via something like:
因此,如果有的话,我会通过以下方式对其进行任何更新:
//Swift
dispatch_async(dispatch_get_main_queue()){
//do what you have to do here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.