2016-10-23 16 views
3

私のコードは次のとおりです。UIPickerViewDelegate Xcodeの8スウィフト3

Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'

どれソリューション:

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])")} 
    } 

このコードはエラーがありますか?

+3

「UIPickerViewDataSource」と「UIPickerViewDelegate」のドキュメントを参照してください。すべてのメソッドはSwift 3で変更されました。 – rmaddy

答えて

12

スウィフト3で行われ、ボタンの完全な例でPickerView スウィフト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) { 

} 
1

PickerViewの正しい構文

  1. まず、SWIFT 3.ドラッグでテキストを単一のビューアプリケーションを作成します。フィールド&はView Controllerに接続します。
  2. numberOfComponentsのようなメソッドを実装する必要があり、numberOfRowsInComponent、titleForRow
  3. ピッカービューのように見える画像 enter image description here
  4. 以下は完全なソースは

    怒鳴る与えられているUIPickerViewDelegate & UIPickerViewDataSource
  5. UIPickerViewDataSource
  6. 、UIPickerViewDelegateという名前の2つのPickerViewプロトコルで実装
    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 
         } 
    
  7. Gに与えられたプロジェクトithub。ギブスリンク:https://github.com/enamul95/PickerView1.git

+0

あなたは大歓迎です.. –

関連する問題