Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created July 3, 2016 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/f88be3580aa8a8f9bab0b20a8d0f6495 to your computer and use it in GitHub Desktop.
Save chuck0523/f88be3580aa8a8f9bab0b20a8d0f6495 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// 011
//
// Created by chuck on 7/3/16.
// Copyright © 2016 chuck. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
private var myUIPicker: UIPickerView!
// 表示する値一覧
private let myValues: NSArray = ["one", "two", "three", "four", "five"]
override func viewDidLoad() {
super.viewDidLoad()
myUIPicker = UIPickerView()
// サイズ指定
myUIPicker.frame = CGRectMake(0,0,self.view.bounds.width, 180.0)
myUIPicker.delegate = self
myUIPicker.dataSource = self
self.view.addSubview(myUIPicker)
}
// pickerに表示する列数を返すデータソースメソッド
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// pickerに表示する行数を返すデータソースメソッド
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myValues.count
}
// pickerに表示する値を返すデリゲートメソッド
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return myValues[row] as? String
}
// pickerが選択された際に呼ばれるデリゲートメソッド
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("row: \(row)")
print("value: \(myValues[row])")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment