我按照Youtube教程在Swift中创建了一个选择器视图,如下所示,但是它有一个错误,如该行标题所示:
activityLevelField.inputView = activityPicker
代码:
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return activityTypes.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return activityTypes[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedActivity = activityTypes[row]
activityLevelField.text = selectedActivity
}
var selectedActivity: String?
var activityTypes = ["Less active", "active", "very active"]
func createActivityPicker(){
let activityPicker = UIPickerView()
activityPicker.delegate = self
activityPicker.dataSource = self
activityLevelField.inputView = activityPicker
}
@IBOutlet weak var activityLevelField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createActivityPicker()
}
}
答案 0 :(得分:0)
您与情节提要的连接断开。从textfiled的连接检查器检查您的连接。