Category Archives: Picker View

Picker View

Picker View

/* here shows how PickerView works. can change background color by selecting on pickerView  */

// Attach Outlets -> dataSource and delegate with view-controller

screen-shot-2016-11-20-at-12-42-14-am

// Created by Nazmul Hasan on 11/19/16.


import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
 
 let moodArray = ["happy","good","better","bad","sad","well","red","green","great","pretty","yellow","nice","cyan","beautiful"]
 
 func numberOfComponents(in pickerView: UIPickerView) -> Int {
 return 1 // indicate pickerView list
 }
 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
 return moodArray.count   // indicate row list
 }
 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
 return moodArray[row]    // indicate array print system
 }
 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
 // it makes background color according to select in row
 
 var newBackGroundColor : UIColor
 
 switch row {
 case 5,8,9,11,13:
 newBackGroundColor = UIColor.lightGray
 case 0...4:
 newBackGroundColor = UIColor.darkGray
 case 6:
 newBackGroundColor = UIColor.red
 case 7:
 newBackGroundColor = UIColor.green
 case 10:
 newBackGroundColor = UIColor.yellow
 case 12:
 newBackGroundColor = UIColor.cyan
 
 default:
 newBackGroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
 }
 self.view.backgroundColor = newBackGroundColor 
// view indicate background
 
 }
 
 override func viewDidLoad() {
 super.viewDidLoad()
 // Do any additional setup after loading the view, typically from a nib.
 }

 override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()
 // Dispose of any resources that can be recreated.
 }

}