Swiftで遊ぼう! on Hatena

あしたさぬきblogでやってた初心者オヤジのiOSプログラミング奮闘記がHatenaに来ました

Swiftで遊ぼう! - 325 - My Picker Project : buttonPressedの挙動

Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide
質問 : Swiftで遊ぼう! - 252 - FaceViewプロジェクト始動 - Swiftで遊ぼう! on Hatena

@IBAction func buttonPressed(sender: AnyObject) {
 let stateRow : Int = 
       dependentPicker.selectedRowInComponent(stateComponent)
 let zipRow = 
       dependentPicker.selectedRowInComponent(zipComponent)
        
 let state = states[stateRow]
 let zip = zips[zipRow]

 let title = "選択したZipコードは\(zip)です。"
 let message = "\(zip)\(state)州にあります。"
        
 let alert = UIAlertController(title: title,
               message: message, preferredStyle: .Alert)
 let action = UIAlertAction(title: "OK",
               style: .Default, handler: nil)

 alert.addAction(action)
 presentViewController(alert,
               animated: true, completion: nil)
}

ボタンを押したときの挙動は理解しやすいので説明はそれほど必要としませんね。

PickerViewオブジェクトのメソッドを使って、何が選択されているのか、選択された項目番号をstateRowとzipRowで確定して、その内容をstateとzipに取り込んで、アラート画面で表示するという単純な作業です。

今日はここまで。