我有2个分类:Game
和ViewController
(NSViewController
的子类),Game
类:
class Game {
required init(num : CGFloat, picName : String, gameName : String, description : String, windowHeight : CGFloat) {
self.number = num
self.pictureName = picName
self.name = gameName
self.desc = description
self.height = windowHeight
}
var number : CGFloat
var pictureName : String
var name : String
var desc : String
var height : CGFloat
var image : NSImage {
return NSImage(named: pictureName)!
}
var imageView : NSImageView {
return NSImageView(frame: NSRect(x: 0, y: height - (110 * (number - 1)) - 100, width: 100, height: 100))
}
var nameField : NSTextField {
return NSTextField(frame: NSRect(x: 110, y: height - (110 * (number - 1)) - 100, width: 300, height: 30))
}
var descField : NSTextField {
return NSTextField(frame: NSRect(x: 110, y: height - (110 * (number - 1)) - 140, width: 300, height: 60))
}
func setImage() {
imageView.image = image
}
func setNameField() {
nameField.font = NSFont(name: "System", size: 15.0)
nameField.stringValue = "Name"
nameField.alignment = NSTextAlignment(rawValue: 2)!
nameField.selectable = false
nameField.editable = false
}
func setDescField() {
descField.selectable = false
descField.editable = false
descField.stringValue = desc
descField.font = NSFont(name: "System", size: 11.0)
}
}
在viewDidAppear
我这样做:
var size = self.view.window?.frame.size
var newGame = Game(num: 1, picName: "Bomb.png", gameName: "Bomber", description: "Just a simpla application", windowHeight: size!.height)
newGame.setDescField()
newGame.setImage()
newGame.setNameField()
println(newGame.descField.editable)
self.view.addSubview(newGame.imageView)
self.view.addSubview(newGame.descField)
self.view.addSubview(newGame.nameField)
descField.editable
总是true
,即使我在课程descField.editable = false
的函数中编写Game
。如何解决?
答案 0 :(得分:1)
编写代码的方式,每次访问nameField时都会生成NSTextField的新实例,依此类推。
您希望使用延迟实例化,这可以通过添加“懒惰”来轻松实现。关键字:
lazy var nameField : NSTextField {
return NSTextField(frame: NSRect(x: 110, y: height - (110 * (number - 1)) - 100, width: 300, height: 30))
}
甚至更简单:
lazy var nameField = NSTextField(frame: NSRect(x: 110, y: height - (110 * (number - 1)) - 100, width: 300, height: 30))
对image,imageView和descField执行相同的操作。
答案 1 :(得分:1)
我找到了解决方案。您需要将所有字段和图像视图声明为惰性变量,并在init
:
import Foundation
import Cocoa
import AppKit
class Game {
init(num : CGFloat, picName : String, gameName : String, description : String, windowHeight : CGFloat, sender : NSViewController) {
self.number = num
self.pictureName = picName
self.name = gameName
self.desc = description
self.height = windowHeight
imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
nameField = NSTextField(frame: NSRect(x: 110, y: 60, width: 300, height: 30))
descField = NSTextField(frame: NSRect(x: 110, y: 0, width: 300, height: 60))
nameField.font = NSFont(name: "System", size: 15.0)
nameField.stringValue = self.name
nameField.alignment = NSTextAlignment(rawValue: 2)!
nameField.selectable = false
nameField.editable = false
nameField.backgroundColor = sender.view.window?.backgroundColor
nameField.bordered = false
descField.selectable = false
descField.editable = false
descField.stringValue = desc
descField.font = NSFont(name: "System", size: 11.0)
descField.backgroundColor = NSColor.controlColor()
descField.bordered = true
descField.backgroundColor = sender.view.window?.backgroundColor
imageView.image = NSImage(named: self.pictureName)!
gameView = NSView(frame: NSRect(x: 0, y: /*height - (100 * num)*/ height - (120 * num), width: 600, height: 100))
button = NSButton(frame: NSRect(x: 0, y: 0, width: 600, height: 100))
button.tag = Int(number)
button.action = nil
var gesture = NSClickGestureRecognizer(target: sender, action: Selector("buttonPressed:"))
gesture.numberOfClicksRequired = 1
gesture.buttonMask = 0x1
button.addGestureRecognizer(gesture)
button.bezelStyle = NSBezelStyle(rawValue: 6)!
button.transparent = true
gameView.addSubview(nameField)
gameView.addSubview(imageView)
gameView.addSubview(descField)
gameView.addSubview(button)
}
var number : CGFloat
var pictureName : String
var name : String
var desc : String
var height : CGFloat
lazy var button : NSButton = NSButton()
lazy var imageView : NSImageView = NSImageView()
lazy var nameField : NSTextField = NSTextField()
lazy var descField : NSTextField = NSTextField()
lazy var gameView : NSView = NSView()
}
然后你就这样称呼这个:
var newGame = Game(num: number, picName: "Bomb2.png", gameName: "Bomber", description: "Just a simple application", windowHeight: size!.height, sender: self)
self.view.addSubview(newGame.gameView)
答案 2 :(得分:0)
这样怎么样:
var descField = NSTextField(frame: NSRect(x: 110, y: height - (110 * (number - 1)) - 140, width: 300, height: 60))
var editable: Bool {
get {
return descField.editable
}
set {
descField.editable = newValue
}
}
你可以使用你的游戏实例:
var newGame //......
newGame.editable = true