UIPickerViewDelegate Xcode 8 Swift 3

喜欢而已 提交于 2019-12-06 00:31:52
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

  1. First Of all, Create a single view application in swift 3. Drag a text field & connect to view controller.
  2. Implement in two PickerView protocol named UIPickerViewDelegate,UIPickerViewDataSource
  3. UIPickerViewDelegate & UIPickerViewDataSource need to implement methods like numberOfComponents,numberOfRowsInComponent,titleForRow
  4. Picker View Look Like Below Image

  5. 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
             }
    
  6. Complete Project Is given to Github. Github Link: https://github.com/enamul95/PickerView1.git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!