2016-10-14 1 views
1
func popUpList(title: String, PickerList: [String],index: Int, complistionHandler: @escaping (_ selectedValue: Int) ->()) { 
    let alertController = UIAlertController(title: nil, message: "\n\n\n\n\n\n\n", preferredStyle: .alert) 
    alertController.isModalInPopover = true; 
    let picker = UIPickerView(frame: CGRect(x: 0.0, y: 0.0, width: 250, height: 250)) 
    picker.delegate = self 
    picker.dataSource = self 
    picker.selectRow(index, inComponent: 0, animated: true) 
    picker.showsSelectionIndicator = true 
    list = PickerList 

    picker.reloadAllComponents() 
    alertController.view.addSubview(picker) 
    let ok = UIAlertAction(title: "Ok", style: .default) { (alert) in 
      print(picker.selectedRow(inComponent: 0)) 
     complistionHandler(picker.selectedRow(inComponent: 0)) 
    } 
    alertController.addAction(ok) 
    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 
    alertController.addAction(cancel) 
    self.present(alertController, animated: true, completion: nil); 
} 

このコードは常に値= 3を返している中をしかし、私は選択されたインデックスの値を取得したいです。助けてくださいここに私を助けてくださいは、それは常に同じインデックス「(inComponent:0)picker.selectedRow」戻っているSWIFTのiOS

答えて

2

私はあなたのコードを試しました。あなたのピッカーが大きすぎることが判明しました。その背景は透明でボタンの前にありますので、すべてのタッチを傍受します。 Yサイズを小さくすると、もはや競合しなくなります。同様に、新しい行を追加すると同様に動作します。また、ボタンの後ろにピッカーを置くと、それも機能します。ビューデバッガを使用すると、ピッカーの範囲を見ることができます。オーバーラッピングとそれをボタンの前に表示できます。このコードの動作:

import UIKit 

    typealias PopUpListCompletionHandler = (Int) -> (Void) 

    class ViewController: UIViewController { 
     fileprivate var list = [String]() 
     override func viewDidAppear(_ animated: Bool) { 
      super.viewDidAppear(animated) 

      popUpList(title: "Pick a number from 1 to 10", PickerList: stride(from: 1, to: 11, by: 1).map {"\($0)"}, index: 5) { index in 
       print ("You picked index \(index) which has a value of \(self.list[index])") 
      } 

     } 


     func popUpList(title: String, PickerList: [String],index: Int, completionHandler: @escaping PopUpListCompletionHandler) { 
      let alertController = UIAlertController(title: nil, message: "\n\n\n\n\n\n\n", preferredStyle: .alert) 
      alertController.isModalInPopover = true; 
      let picker = UIPickerView(frame: CGRect(x: 0.0, y: 0.0, width: 250, height: 200)) 
      picker.delegate = self 
      picker.dataSource = self 
      picker.selectRow(index, inComponent: 0, animated: true) 
      picker.showsSelectionIndicator = true 
      list = PickerList 

      picker.reloadAllComponents() 
      alertController.view.insertSubview(picker, at: 1) 
      let ok = UIAlertAction(title: "Ok", style: .default) { (alert) in 
       completionHandler(picker.selectedRow(inComponent: 0)) 
       self.dismiss(animated: true, completion: nil) 
      } 
      alertController.addAction(ok) 
      let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 
      alertController.addAction(cancel) 
      self.present(alertController, animated: true, completion: nil); 
     } 

    } 

    extension ViewController: UIPickerViewDelegate { 

    } 

    extension ViewController: UIPickerViewDataSource { 
     func numberOfComponents(in pickerView: UIPickerView) -> Int { 
      return 1 
     } 

     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
      return list.count 
     } 

     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
      return list[row] 
     } 
    } 
+0

ありがとう:) – hatim

関連する問題