최근 프로필과 같은 일부 속성에 대한 데이터를 선택하기 위해 현재와 같은 pickerView를 보여주는 솔루션이 필요합니다. 내가 한 것은 입니다. 1, 빈 프로젝트를 만듭니다 2, 하위 뷰로 사용될 AViewController를 만듭니다. 그리고 pickerView와 탐색 막대를 해당 뷰에 추가하고 뷰의 높이를 약 260으로 조정합니다. 3, AViewController 클래스에 코드를 작성합니다.UIPickerView를 서브 뷰로 추가하고, 매우 유익합니다.
class APickerViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var pickerView:UIPickerView!
let dataList = [["a","b","c","d"]]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//pickerView.dataSource = self
//pickerView.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return dataList.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataList[component][row]
}
4 주 뷰 컨트롤러에 코드를 작성하여 AViewController의 뷰를 하위 뷰로 추가하십시오.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let pickerViewController = storyboard?.instantiateViewController(withIdentifier: String(APickerViewController.self)) as! APickerViewController
self.view.addSubview(pickerViewController.view)
self.view.bringSubview(toFront: pickerViewController.view)
pickerViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - 300, width: self.view.frame.width, height: 300)
}
여기에 한 가지 질문이 있습니다. 실제로 차이는 확실하지 않습니다. A :
let pickerViewController = storyboard?.instantiateViewController(withIdentifier: String(APickerViewController.self)) as! APickerViewController
B :
A와 B의 다른 무엇let pickerViewController = APickerViewController()
? Interface Builder에 의해 @IBOutlet 설정이 영향을 받거나 무엇을해야합니까?
5, 가서 ...이 weired 얻을 ...
기능을
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}
언젠가의이 호출되는 여러 번 때로는 전화를받을 수 없습니다.
데이터가 pickerView에 표시되지 않고 행 1,2가 표시되는 경우가 있으며 위나 아래로 넘기는 경우 1,2 행 데이터가 사라집니다.
누구나 pickerView를 옵션 팝업 창으로 사용하는 더 좋은 해결책이 있다면?
도움말.
감사 도구 모음 내부
세트 선택기. –
안녕하세요, 파텔, 당신의 이상에 대해 더 자세히 설명해 주시겠습니까? –