2017-07-02 53 views
0

在下面的代碼中,我們得到錯誤「buttonWithType」不可用:在下面的代碼行中使用對象構造「UIButton(type :)」幫助按鈕「self.hintButton = UIButton.buttonWithType(.custom)。有什麼建議麼。謝謝。buttonWithType'is unavailable:use object construction'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 
    } 

} 
+0

編譯器給你一個建議。你試過了嗎? – vadian

+0

我試過,但得到「buttonWithType已明確標記爲不可用」。不知道該怎麼辦。 – BiHMaster

+0

請閱讀完整的錯誤消息**'使用對象構造'UIButton(type:)'** – vadian

回答

1

只是轉換這個

self.hintButton = UIButton.buttonWithType(.custom) 

這個

self.hintButton = UIButton(type: .custom) 
+1

非常好。奇蹟般有效。是的,使用Swift 3.謝謝。 – BiHMaster

0

寫您的按鈕初始化爲編譯器建議

使用對象的構造「的UIButton(類型:)

self.hintButton = UIButton(type: .custom) 
相關問題