[iOS] 데이터 픽커(Data Picker) 만들기
필그램
·2017. 8. 17. 01:59
iOS 프로그램에서 데이터 픽커(Data Picker) 만들기 위해서
1단계 : 먼저 스토리 보드에 아래 그림처럼 데이터 픽커(Data Picker)를 올려 놓습니다.
2단계 : Deligate을 상속합니다.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
let items:[String] = ["Korea", "US", "Canada", "France", "China", "Brazil"]
// 위 2개를 상속 시키고, 아래 배열을 추가(1개씩 출력)
// 이 배열 내용이 데이터 픽커에 출력됩니다
override func viewDidLoad() {
super.viewDidLoad()
}
//3단계 : 픽커뷰는 3개의 function을 추가해야 한다.
// 한줄에 나올 콤포넌트 갯수 number of components, 전체 리스트 갯수를 넣는곳 number of rows in component
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return items[row]
}
//===========
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
실행결과는 다음과 같습니다.
'프로그래밍 > 모바일: iOS, Java, Android, Swift' 카테고리의 다른 글
[iOS] 이미지 갤러리 간략소스 (0) | 2017.08.19 |
---|---|
[iOS] segment(세그먼트) 간략소스 (0) | 2017.08.19 |
[Java] 자바 배열 전체 출력하는 5가지 방법 (Arraylist이용) : for문, iterator 사용 (0) | 2017.08.15 |
[iOS 기초] 텍트스 입력 후, 키보드 없애기 (0) | 2017.08.14 |
[iOS] Xcode 시뮬레이터에서 키보드 보는법 (0) | 2017.08.14 |