2017-03-23 70 views
-1

我與UISlider工作,我想認識價值的變化:無法識別的選擇發送到實例,UISlider swfit3

var sliderGeofence: UISlider! 

裏面我viewDidLoad()

self.sliderGeofence = UISlider(frame: CGRect(x: 20, y: 20, width: self.view.frame.size.width - 50, height: 50)) 
     self.sliderGeofence?.maximumValue = 100000 
     self.sliderGeofence?.minimumValue = 500 
     self.sliderGeofence?.value = 100 
     self.sliderGeofence?.isUserInteractionEnabled=true 
     self.sliderGeofence?.addTarget(self, action: Selector("geofenceValueChange:"),for: UIControlEvents.valueChanged) 
     gmsMap.addSubview(self.sliderGeofence!) 
     sliderGeofence?.isHidden = true 

我的價值變化的方法:

@IBAction func geofenceValueChange(sender: AnyObject) 
    { 
    print("entro") 
    } 

該應用程序加載滑塊,但是,當我更改其值時發生這種情況:

***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: ' - [MocaAdmin.FirstViewController geofenceValueChange:]:無法識別的選擇發送到實例 0x7fca61511480'

我在做什麼錯誤?

+1

嘗試更改添加目標到:'self.sliderGeofence?.addTarget(self,action:#selector(geofenceValueChange :),for:UIControlEvents.valueChanged)' – Priyal

+1

@Priyal,你在那裏。見下面的Rashwan的答案。選擇器的Swift形式將是'geofenceValueChange(_ :)' –

回答

3

改變目標爲以下內容:

sliderDemo.addTarget(self, action: #selector(geofenceValueChange(_:)), for: .valueChanged) 

然後是功能:

func geofenceValueChange(_ sender:UISlider) { 
    print("entro") 
} 

無需在函數中@IBAction

2

您應該使用選擇這樣的:

self.sliderGeofence?.addTarget(self, action: #selector(FirstViewController.geofenceValueChange(_:)), for: .valueChanged) 

這樣,Xcode中甚至會建議功能使用,並自動完成您的輸入,以確保事情會工作。

希望有幫助!

相關問題