如何子类化UIbutton并添加一些变量

时间:2017-01-22 11:43:02

标签: ios swift xcode

我想编写一个继承自UIbutton的类,其中一个init传入更多属性,如下面的代码所示,但始终存在错误,请帮我解决。

另一种方法是实现这种效果。 这是第一次错误的截图enter image description here,然后我尝试回答代码可以成功编译,但在运行时错误,我已更新错误enter image description here的上述截图 非常感谢你

import UIKit

class myButton:UIButton {

    var x = 0
    var y = 0
    var Px = 0
    var Py = 0
    var g = 0.0
    var f = 999.5
    var cango: Bool
    var zuobiao = [0,0]


    init(x: Int, y: Int,Px: Int,Py: Int,g:Double,f:Double,cango:Bool,zuobiao:[Int]){
        self.x = x
        self.y = y
        self.Px = Px
        self.Py = Py
        self.g = g
        self.f = f
        self.cango = cango
        self.zuobiao = zuobiao
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

1 个答案:

答案 0 :(得分:0)

您的问题的简单解决方案是使用init(frame: CGRect)

class myButton: UIButton {
    var x = 0
    var y = 0
    var px = 0
    var py = 0
    var g = 0.0
    var f = 999.5
    var cango: Bool
    var zuobiao = [0, 0]

    init(frame: CGRect, x: Int, y: Int, px: Int, py: Int, g: Double, f: Double , cango: Bool, zuobiao: [Int]) {
        self.x = x
        self.y = y
        self.px = px
        self.py = py
        self.g = g
        self.f = f
        self.cango = cango
        self.zuobiao = zuobiao
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

但看起来需要重构变量......