Ios 按钮类型为';不可用:使用对象构造';UIButton(类型:)
在下面的代码中,我们得到错误“buttonWithType”不可用:在“help button”Ios 按钮类型为';不可用:使用对象构造';UIButton(类型:),ios,swift,Ios,Swift,在下面的代码中,我们得到错误“buttonWithType”不可用:在“help button”self.hintButton=UIButton.buttonWithType(.custom)下面的代码行中使用对象构造“UIButton(type:)”。任何建议。谢谢 class HUDView: UIView { var stopwatch: StopwatchView var gamePoints: CounterLabelView var hi
self.hintButton=UIButton.buttonWithType(.custom)
下面的代码行中使用对象构造“UIButton(type:)”。任何建议。谢谢
class HUDView: UIView {
var stopwatch: StopwatchView
var gamePoints: CounterLabelView
var hintButton: UIButton!
//this should never be called
required init(coder aDecoder:NSCoder) {
fatalError("use init(frame:")
}
override init(frame:CGRect) {
self.stopwatch = StopwatchView(frame:CGRect(x: ScreenWidth/2-150, y: 0, width: 300, height: 100))
self.stopwatch.setSeconds(0)
//the dynamic points label
self.gamePoints = CounterLabelView(font: FontHUD, frame: CGRect(x: ScreenWidth-200, y: 30, width: 200, height: 70))
gamePoints.textColor = UIColor(red: 0.38, green: 0.098, blue: 0.035, alpha: 1)
gamePoints.value = 0
super.init(frame:frame)
self.addSubview(gamePoints)
//"points" label
let pointsLabel = UILabel(frame: CGRect(x: ScreenWidth-340, y: 30, width: 140, height: 70))
pointsLabel.backgroundColor = UIColor.clear
pointsLabel.font = FontHUD
pointsLabel.text = " Points:"
self.addSubview(pointsLabel)
self.addSubview(self.stopwatch)
self.isUserInteractionEnabled = true
//load the button image
let hintButtonImage = UIImage(named: "btn")!
//the help button
self.hintButton = UIButton.buttonWithType(.custom)
hintButton.setTitle("Hint!", for:UIControlState())
hintButton.titleLabel?.font = FontHUD
hintButton.setBackgroundImage(hintButtonImage, for: UIControlState())
hintButton.frame = CGRect(x: 50, y: 30, width: hintButtonImage.size.width, height: hintButtonImage.size.height)
hintButton.alpha = 0.8
self.addSubview(hintButton)
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
//1 let touches through and only catch the ones on buttons
let hitView = super.hitTest(point, with: event)
//2
if hitView is UIButton {
return hitView
}
//3
return nil
}
}
把这个换成
self.hintButton = UIButton.buttonWithType(.custom)
对此
self.hintButton = UIButton(type: .custom)
按照编译器的建议编写按钮初始化 “使用对象构造”按钮(类型:)
编译器会给你一个建议。你试过了吗?我试过了,但得到的是“buttonWithType已明确标记为此处不可用”。不确定该怎么办。请阅读整个错误消息
use object construction'ui按钮(type:)
不要通过编辑和询问完全不同的问题来破坏问题。问一个新问题。是的,你的原始问题与链接问题完全相同。太好了。工作起来很有魅力。是的,使用Swift 3。谢谢。
self.hintButton = UIButton(type: .custom)