Advertisement
thieumao

PickerView demo

May 16th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.49 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  4.  
  5.     @IBOutlet weak var pickerView: UIPickerView!
  6.     let colors = ["xanh", "do", "vang", "den"]
  7.    
  8.     override func viewDidLoad() {
  9.         super.viewDidLoad()
  10.         // Do any additional setup after loading the view, typically from a nib.
  11.     }
  12.  
  13.     override func didReceiveMemoryWarning() {
  14.         super.didReceiveMemoryWarning()
  15.         // Dispose of any resources that can be recreated.
  16.     }
  17.  
  18.     func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  19.         return colors.count
  20.     }
  21.    
  22.     func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  23.         return 50
  24.     }
  25.    
  26.     func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  27.         return colors[row]
  28.     }
  29.    
  30.     func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
  31.         return 1
  32.     }
  33.    
  34.     func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  35.         if (row == 0) {
  36.             self.view.backgroundColor = UIColor.blueColor()
  37.         } else if (row == 1) {
  38.             self.view.backgroundColor = UIColor.redColor()
  39.         } else if (row == 2) {
  40.             self.view.backgroundColor = UIColor.yellowColor()
  41.         } else {
  42.             self.view.backgroundColor = UIColor.blackColor()
  43.         }
  44.     }
  45.    
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement