Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Swift 如何创建可重用的可设计代码_Swift_Xcode_Ibdesignable - Fatal编程技术网

Swift 如何创建可重用的可设计代码

Swift 如何创建可重用的可设计代码,swift,xcode,ibdesignable,Swift,Xcode,Ibdesignable,当使用IBDesignable时,以下代码很常见,每次创建类时我都会重复,有没有办法避免这种重复 override init(frame: CGRect) { super.init(frame: frame) themeProp() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) themeProp() } override func prepareForInt

当使用IBDesignable时,以下代码很常见,每次创建类时我都会重复,有没有办法避免这种重复

override init(frame: CGRect) {
    super.init(frame: frame)

    themeProp()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    themeProp()
}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()

    themeProp()
}
这就是我目前使用IBDesignable为UIButton创建样式的方式

import UIKit

let colorWhite = colorLiteral(red: 0.9999127984, green: 1, blue: 0.9998814464, alpha: 1)
let colorLavender = colorLiteral(red: 0.6604440808, green: 0.5388858914, blue: 0.8827161193, alpha: 1)

@IBDesignable class PrimaryButtonA: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)

        themeProp()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        themeProp()
    }

    func themeProp() {
        setTitleColor(colorWhite, for:.normal)
        self.layer.cornerRadius = 10
        backgroundColor = colorLavender
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()

        themeProp()
    }
}


@IBDesignable class PrimaryButtonB: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)

        themeProp()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        themeProp()
    }

    func themeProp() {
        setTitleColor(colorWhite, for:.normal)
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()

        themeProp()
    }
}
以我有限的知识,我尝试创建一个函数,并尝试在每个类中调用它,但它不起作用

在每个类声明中重复这12行代码没有任何意义。因此,如果有办法避免这种重复,那么请使用我的代码作为答案


谢谢

一个可能的解决方案是为这些视图创建一个公共超类。唯一的缺点是必须为每种类型创建一个超类(
UIView
s、
UIButton
s等)


然后,将可设计类设置为
DesignableView
的子类。您只需覆盖
themeProp()

一个可能的解决方案是为这些视图创建一个公共超类。唯一的缺点是必须为每种类型创建一个超类(
UIView
s、
UIButton
s等)


然后,将可设计类设置为
DesignableView
的子类。您只需覆盖
themeProp()

我们是否可以创建一个
UIControl
的超级类,以使其对所有视图(如
UIView
UIButton
等)通用?@Hemang您可以,但您很快就会发现自己不断地将视图转换为另一种类型,因为它将是一个
ui视图
,而不是
ui按钮
。你是对的,在这种情况下,最好有单独的类,如果这符合应用程序的需求和长期增长的价值。谢谢!塔马斯·森格尔。我试过你的答案,效果很好。难道我们不能创建一个超级类的
UIControl
,使它成为所有视图的通用视图,如
UIView
UIButton
等吗?@Hemang你可以,但你很快就会发现自己不断地将视图转换为另一种类型,因为它将是一个
ui视图
,而不是
ui按钮
。你是对的,在这种情况下,最好有单独的类,如果这符合应用程序的需求和长期增长的价值。谢谢!塔马斯·森格尔。我尝试了你的答案,效果非常好。不需要
prepareforPrinterFaceBuilder
。如果只需要在IB有一些特殊配置的情况下才需要它。您通常不需要它(调用
init
方法,即使从IB实例化)。您当然不应该在那里调用
themeProp
。通常,
override init(frame:CGRect=.zero){…}
required init?(coder-aDecoder:NSCoder){…}
就足够了……不需要
prepareforprinterfacebuilder
。如果只需要在IB有一些特殊配置的情况下才需要它。您通常不需要它(调用
init
方法,即使从IB实例化)。您当然不应该在那里调用
themeProp
。通常
重写init(frame:CGRect=.zero){…}
所需的init?(coder-aDecoder:NSCoder){…}
就足够了。。。
class DesignableView: UIView {
    override init(frame: CGRect) {  
        super.init(frame: frame)  
        themeProp()  
    }  

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

    override func prepareForInterfaceBuilder() {  
        super.prepareForInterfaceBuilder()  
        themeProp()  
    }

    func themeProp() { }
}