8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSアプリ開発/textFieldでPickerView(ドラムロール)を実装/Swift

Last updated at Posted at 2015-05-23

参考

http://suxisuxido.com/uitextfield_uipickerview/
http://swiftdev.blog.fc2.com/blog-entry-13.html
[連続番号の実装]
(http://qiita.com/moonkids/items/ea2488e78800c520746a)

1つのviewで複数のPickeViewを実装

※覚書なのでエラー処理はなし

ViewContoroller.swift
class ViewController: UIViewController {
    var textField: UITextField!
    @IBOutlet weak var ageField: UITextField!
    @IBOutlet weak var genderField: UITextField!
    var selectLanguage: String = "ja"
        
    func setField(){
    	self.view.addSubview(ageField)
    	self.view.addSubview(genderField)
    	
    	// gender用のpickerView
    	var genderPickerView = UIPickerView()
       	genderPickerView.showsSelectionIndicator = true
       	genderPickerView.delegate = self
       genderField.inputView = genderPickerView
       genderPickerView.tag = 1
       
       // age用のpickerView
       var agePickerView = UIPickerView()
       agePickerView.showsSelectionIndicator = true
       agePickerView.delegate = self
       ageField.inputView = agePickerView
       agePickerView.tag = 2
       
       // toolBarの実装 
      	toolBar = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height/6, self.view.frame.size.width, 40.0))
        toolBar.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)
        toolBar.barStyle = .BlackTranslucent
        toolBar.tintColor = UIColor.whiteColor()
        toolBar.backgroundColor = UIColor.blackColor()
        let toolBarBtn = UIBarButtonItem(title: "完了", style: .Done, target: self, action: "tappedToolBarBtn:")
        toolBarBtn.tag = 1
        toolBar.items = [toolBarBtn]
        
        genderField.inputAccessoryView = toolBar
        ageField.inputAccessoryView = toolBar
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        var pickCount: Int
        // tagによってpickerViewを切り替えている
        if pickerView.tag == 1 {
        	// 言語によってドラムロールへの表示内容を変えている
        	switch selectLanguage {
           	case "ja":
              		pickCount = count(AppConstants.gendersJa)
          	case "en":
           		pickCount = count(AppConstants.gendersEn)
           	default:
           		//error
            }
        } else {
        	pickCount = count(AppConstants.ageRange)
        }
        return pickCount
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        var pickString: String

        if pickerView.tag == 1 {
            switch selectLanguage {
            case "ja":
                pickString = AppConstants.gendersJa[row].0
            case "en":
                pickString = AppConstants.gendersEn[row].0
            default:
                // error
            }
        } else {
        	pickString = String(AppConstants.ageRange[row])
        }
        return pickString
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView.tag == 1 {
            switch selectLanguage {
                case "ja":
                    genderField.text = AppConstants.gendersJa[row].0
                case "en":
                    genderField.text = AppConstants.gendersEn[row].0
                default:
                    // error
            }
        } else {
        	ageField.text = String(AppConstants.ageRange[row])
        }
    }
    
    // toolbarをタップすると、field選択を解除
    func tappedToolBarBtn(sender: UIBarButtonItem) {
        genderField.resignFirstResponder()
        ageField.resignFirstResponder()
    }

定数用ファイルを作成

AppConstants.swift
struct AppConstants {
    static let gendersEn:[(String,Int)] = [
        ("",0),
        ("male",1),
        ("female",2)
    ]
    static let gendersJa:[(String,Int)] = [
        ("",0),
        ("男性",1),
        ("女性",2)
    ]
    
    static let ageRange = (Array<Int>)(10...80) //連続した数値を配列で用意
    
    enum language: Int{
        case en = 0
        case ja = 1
    }
}
8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?