Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 需要帮助获得可设计的工作吗_Objective C_Cocoa_Swift_Ibdesignable - Fatal编程技术网

Objective c 需要帮助获得可设计的工作吗

Objective c 需要帮助获得可设计的工作吗,objective-c,cocoa,swift,ibdesignable,Objective C,Cocoa,Swift,Ibdesignable,我无法让我的自定义按钮实时预览基本示例,这让我推迟使用对我的开发IBDesignable有很大帮助的东西 我的自定义按钮代码如下所示: import Cocoa @IBDesignable class MyButton: NSButton { @IBInspectable var name:String = "Bob"{ didSet{ setup() } } override init(frame frameR

我无法让我的自定义按钮实时预览基本示例,这让我推迟使用对我的开发IBDesignable有很大帮助的东西

我的自定义按钮代码如下所示:

import Cocoa
@IBDesignable class MyButton: NSButton {

    @IBInspectable var name:String = "Bob"{
        didSet{
            setup()
        }
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    override func prepareForInterfaceBuilder() {
        setup()
    }

    func setup(){
        self.title = name
        self.setNeedsDisplay()
    }

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        // Drawing code here.
    }

}
然后将自定义视图或NSButton拖到我的canvas mainmenu.xib上,并在inspector窗口中将其类类型调整为MyButton。可检查字段弹出,没有错误,但是当我在属性面板中更改其值时,我的自定义按钮不会更改其名称

此外,将类更改为MyButton后,当我将自定义视图拖到画布上时,我得到的只是一个空白/透明的矩形,而不是按钮


如蒙协助,将不胜感激。这让我快发疯了

我必须将按钮放在NSView中:

然后我就这样使用它:

@IBOutlet weak var myButton: MyButton!

override func viewDidLoad() {
    super.viewDidLoad()

    myButton.touchUpHandler = { [unowned self] in
        self.performSomeAction()
    }
}

请尝试self.setTitlename,forState:.Normal而不是self.title=name。此外,请将常规按钮拖动到情节提要而不是自定义视图,并更改其自定义类。NSButton没有setTitle方法。标题文档说明它为正常状态设置标题。我试着把一个普通的按钮拖到画布上——它不起作用。我开始怀疑这只适用于NSView类…抱歉,我错误地将NSButton理解为UIButton,并认为这是iOS开发:
@IBOutlet weak var myButton: MyButton!

override func viewDidLoad() {
    super.viewDidLoad()

    myButton.touchUpHandler = { [unowned self] in
        self.performSomeAction()
    }
}