所以我在
Swift中学到了我们应该使用两阶段初始化:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html
其中一条规则是:“初始化程序无法调用任何实例方法,读取任何实例属性的值,或者在初始化的第一阶段完成之后将self作为值引用.”
这可以防止在实例方法中放置代码块,所以我想应该使用类方法?你怎么看?任何替代方案?
为了说明我的意思,这里有3个代码示例:
未编码的代码:
@objc class MYChatBarButton: UIBarButtonItem {
let badgeLabel: UILabel
init(target: AnyObject, selector: Selector) {
let button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(0, 0, 35, 44)
button.addTarget(target, action: selector, forControlEvents: .TouchUpInside)
button.tintColor = UIColor.whiteColor()
button.setImage(UIImage(named: "chat_icon")?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 7, bottom: 5, right: 0)
badgeLabel = UILabel(frame: CGRectMake(0, 0, 15, 15))
badgeLabel.backgroundColor = UIColor.clearColor()
badgeLabel.textColor = UIColor.whiteColor()
badgeLabel.textAlignment = .Center
badgeLabel.text = "0"
badgeLabel.userInteractionEnabled = false
button.addSubview(badgeLabel)
super.init(customView: button)
}
}
我希望做重构的方式,但它不起作用:
@objc class MYChatBarButton: UIBarButtonItem {
let badgeLabel: UILabel
init(target: AnyObject, selector: Selector) {
let button = createButton(target, selector: selector)
createBadgeLabel()
button.addSubview(badgeLabel)
super.init(customView: button)
}
func createButton(target: AnyObject, selector: Selector) -> UIButton {
let button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(0, 0, 35, 44)
button.addTarget(target, action: selector, forControlEvents: .TouchUpInside)
button.tintColor = UIColor.whiteColor()
button.setImage(UIImage(named: "chat_icon")?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 7, bottom: 5, right: 0)
return button
}
func createBadgeLabel() {
badgeLabel = UILabel(frame: CGRectMake(0, 0, 15, 15))
badgeLabel.backgroundColor = UIColor.clearColor()
badgeLabel.textColor = UIColor.whiteColor()
badgeLabel.textAlignment = .Center
badgeLabel.text = "0"
badgeLabel.userInteractionEnabled = false
}
}
应该做的方式(但我不太喜欢):
@objc class MYChatBarButton: UIBarButtonItem {
let badgeLabel: UILabel
init(target: AnyObject, selector: Selector) {
let button = MYChatBarButton.createButton(target, selector: selector)
badgeLabel = MYChatBarButton.createBadgeLabel()
button.addSubview(badgeLabel)
super.init(customView: button)
}
class func createButton(target: AnyObject, selector: Selector) -> UIButton {
let button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(0, 0, 35, 44)
button.addTarget(target, action: selector, forControlEvents: .TouchUpInside)
button.tintColor = UIColor.whiteColor()
button.setImage(UIImage(named: "chat_icon")?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 7, bottom: 5, right: 0)
return button
}
class func createBadgeLabel() -> UILabel {
let bl = UILabel(frame: CGRectMake(0, 0, 15, 15))
bl.backgroundColor = UIColor.clearColor()
bl.textColor = UIColor.whiteColor()
bl.textAlignment = .Center
bl.text = "0"
bl.userInteractionEnabled = false
}
}
最佳答案 至少,您可以隔离badgeLabel创建:
@objc class MYChatBarButton: UIBarButtonItem {
let badgeLabel: UILabel = {
let bl = UILabel(frame: CGRectMake(0, 0, 15, 15))
bl.backgroundColor = UIColor.clearColor()
bl.textColor = UIColor.whiteColor()
bl.textAlignment = .Center
bl.text = "0"
bl.userInteractionEnabled = false
return bl
}()
let button: UIButton = {
let button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(0, 0, 35, 44)
button.tintColor = UIColor.whiteColor()
button.setImage(UIImage(named: "chat_icon")?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 7, bottom: 5, right: 0)
return button
}()
init(target: AnyObject, selector: Selector) {
button.addTarget(target, action: selector, forControlEvents: .TouchUpInside)
button.addSubview(badgeLabel)
super.init(customView: button)
}
没有财产,我不知道怎么做.