Swift3 为UISegmentedControl Swift 3分配目标
我有一个Swift3 为UISegmentedControl Swift 3分配目标,swift3,Swift3,我有一个UISegmentedControl,旨在在3种地图视图“标准”、“混合”和“卫星”之间切换。我在“.addTarget”行上遇到以下错误 “源文件中的编辑器占位符” 请尝试一下……这里发生了一些事情。您需要使用一个#选择器并更改函数的声明以抑制第一个参数命名,否则您还必须在选择器中声明它 let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"]) segme
UISegmentedControl
,旨在在3种地图视图“标准”、“混合”和“卫星”之间切换。我在“.addTarget”行上遇到以下错误
“源文件中的编辑器占位符”
请尝试一下……这里发生了一些事情。您需要使用一个#选择器并更改函数的声明以抑制第一个参数命名,否则您还必须在选择器中声明它
let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])
segmentedControl.backgroundColor = UIColor.white.withAlphaComponent(0.5)
segmentedControl.selectedSegmentIndex = 0
// EVENT LISTENER FOR SEGMENT CONTROL
segmentedControl.addTarget(nil, action: #selector(mapTypeChanged(_:)), for: .valueChanged)
func mapTypeChanged(_ segControl: UISegmentedControl){
switch segControl.selectedSegmentIndex{
case 0: mapView.mapType = .standard
case 1: mapView.mapType = .hybrid
case 2: mapView.mapType = .satellite
default: break
}
}
“在声明之前使用局部变量'mapTypeChanged'”您将在何处插入此代码?在ViewController内部?在UIViewController的loadView()内部将其移动到“viewDidLoad”我正在以编程方式创建地图视图,因此它是否应该不在loadView中\
let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])
segmentedControl.backgroundColor = UIColor.white.withAlphaComponent(0.5)
segmentedControl.selectedSegmentIndex = 0
// EVENT LISTENER FOR SEGMENT CONTROL
segmentedControl.addTarget(self, action: "mapTypeChanged:", for: .valueChanged)
func mapTypeChanged() //removing the auto complete params
{
switch segControl.selectedSegmentIndex{
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .hybrid
case 2:
mapView.mapType = .satellite
default:
break
}
}
let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])
segmentedControl.backgroundColor = UIColor.white.withAlphaComponent(0.5)
segmentedControl.selectedSegmentIndex = 0
// EVENT LISTENER FOR SEGMENT CONTROL
segmentedControl.addTarget(nil, action: #selector(mapTypeChanged(_:)), for: .valueChanged)
func mapTypeChanged(_ segControl: UISegmentedControl){
switch segControl.selectedSegmentIndex{
case 0: mapView.mapType = .standard
case 1: mapView.mapType = .hybrid
case 2: mapView.mapType = .satellite
default: break
}
}