My code is:
class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picherview: UIPickerView!
var prodotti = ["AAAAA", "VVVV", "CCCC"]
override func viewDidLoad() {
super.viewDidLoad()
self.picherview.dataSource = self
self.picherview.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerview( _ : UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return prodotti.count
}
func pickerview( _ : UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return prodotti[row]
} //ci consente di determinare quale riga è stata selezionata
func pickerview(_ : UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("Hai selezionato: \(prodotti[row])")}
}
This code have error:
Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'
Any solution?
Rob
Correct syntax of PickerView Swift 3
// DataSource
func numberOfComponents(in pickerView: UIPickerView) -> Int {
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
}
// Delegate
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
PickerView with done button complete example in Swift 3
- First Of all, Create a single view application in swift 3. Drag a text field & connect to view controller.
- Implement in two PickerView protocol named UIPickerViewDelegate,UIPickerViewDataSource
- UIPickerViewDelegate & UIPickerViewDataSource need to implement methods like numberOfComponents,numberOfRowsInComponent,titleForRow
- Picker View Look Like Below Image
Full source are given bellow
class ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { //Text field Connect @IBOutlet weak var txtPickerTextField: UITextField! //Array declare let pickerArray = ["Bangladesh","India","Pakistan","USA"] var selectedRow = 0; //Picker View Object let picker = UIPickerView() override func viewDidLoad() { super.viewDidLoad() //Don't forget to initialize pickerview delegate & datasource picker.delegate = self picker.dataSource = self txtPickerTextField.inputView = picker //Done Button function called doneButton(); } public func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerArray.count } public func pickerView(_pickerView:UIPickerView,numberOfRowsInComponent component: Int) -> Int { return pickerArray.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerArray[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { selectedRow = row; txtPickerTextField.text = pickerArray[row] self.view.endEditing(false) } //begin Doen Button function func doneButton(){ let pickerView = picker pickerView.backgroundColor = .white pickerView.showsSelectionIndicator = true var toolBar = UIToolbar() toolBar.barStyle = UIBarStyle.default toolBar.isTranslucent = true toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) toolBar.sizeToFit() let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donePicker") let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "canclePicker") toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) toolBar.isUserInteractionEnabled = true txtPickerTextField.inputView = pickerView txtPickerTextField.inputAccessoryView = toolBar } func donePicker() { self.txtPickerTextField.text = pickerArray[selectedRow] txtPickerTextField.resignFirstResponder() } func canclePicker() { txtPickerTextField.resignFirstResponder() } //End Doen Button function }
Complete Project Is given to Github. Github Link: https://github.com/enamul95/PickerView1.git
来源:https://stackoverflow.com/questions/40199826/uipickerviewdelegate-xcode-8-swift-3