2016-09-15 86 views
1

我已創建自定義按鈕類並覆蓋所有觸摸方法。它適用於swift 2Xcode 7.3.1。但是,當我在Xcode 8.0打開相同的應用程序,它會顯示錯誤:'CustomButton'類型的值沒有成員'touchDown'

類型的值「的CustomButton」沒有成員「着陸」

類型的值「的CustomButton」沒有成員「touchUpInside」

值類型「的CustomButton」的沒有成員「touchDragExit」

類型的值「的CustomButton」沒有成員「touchDragEnter」

類型自定義的「價值按鈕」沒有成員‘touchCancel’

這裏是我的代碼:

import UIKit 

@IBDesignable 
@objc public class CustomButton: UIButton { 

    private func addTargets() { 

     //------ add target ------- 

     self.addTarget(self, action: #selector(self.touchDown(_:)), for: UIControlEvents.TouchDown) 
     self.addTarget(self, action: #selector(self.touchUpInside(_:)), for: UIControlEvents.TouchUpInside) 
     self.addTarget(self, action: #selector(self.touchDragExit(_:)), for: UIControlEvents.TouchDragExit) 
     self.addTarget(self, action: #selector(self.touchDragEnter(_:)), for: UIControlEvents.TouchDragEnter) 
     self.addTarget(self, action: #selector(self.touchCancel(_:)), for: UIControlEvents.TouchCancel) 
    } 

    func touchDown(sender: CustomButton) { 
     self.layer.opacity = 0.4 
    } 

    func touchUpInside(sender: CustomButton) { 
     self.layer.opacity = 1.0 
    } 

    func touchDragExit(sender: CustomButton) { 
     self.layer.opacity = 1.0 
    } 

    func touchDragEnter(sender: CustomButton) { 
     self.layer.opacity = 0.4 
    } 

    func touchCancel(sender: CustomButton) { 
     self.layer.opacity = 1.0 
    } 
} 

如果任何人有任何解決方案,請讓我知道。

回答

1

如果你想保持你的方法標頭,在你的代碼,您需要更改選擇引用#selector(touchDown(sender:)),等等。

(一般情況下,你沒有必要前綴self.。)

記住所有的函數和方法現在有他們的第一個參數一致的標籤處理。 SE-0046

(您可能會發現很多好文章,以「swift3選擇」搜索)

如果你想保留選擇的參考,您需要更改,如方法:

func touchDown(_ sender: CustomButton) { 

另外,如果你的班級只有一個touchDown(...)方法,#selector(touchDown)會起作用。

+0

感謝....它的工作原理... – VRAwesome

0

我發現解決方案爲@OOPer建議,但也需要在小型情況下更改UIControlEvents。在Xcode 7.3.1這是UIControlEvents.TouchDown,但現在它必須是UIControlEvents.touchDown

它會像:

self.addTarget(self, action: #selector(touchDown(sender:)), for: UIControlEvents.touchDown) 
self.addTarget(self, action: #selector(touchUpInside(sender:)), for: UIControlEvents.touchUpInside) 
self.addTarget(self, action: #selector(touchDragExit(sender:)), for: UIControlEvents.touchDragExit) 
self.addTarget(self, action: #selector(touchDragEnter(sender:)), for: UIControlEvents.touchDragEnter) 
self.addTarget(self, action: #selector(touchCancel(sender:)), for: UIControlEvents.touchCancel) 
相關問題