Tuesday, 7 March 2017

PickerView : Dynamic




//
//  ViewController.swift
//  TestApp1
//
//  Created by Divakar Singh on 3/1/17.
//  Copyright (c) 2017 Divakar Singh. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
   
    var picker1 : UIPickerView!
    var name = ["India","Aus", "US","Rus"]
    var label1 : UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      
        // create label
     label1 = UILabel()
        label1.frame = CGRect(x: 100, y: 10, width: 300, height: 36)
        label1.text = "Default"
        view.addSubview(label1)
        
        picker1 = UIPickerView()
        picker1.center = CGPoint(x: 100, y: 100)
        picker1.sizeToFit()
        picker1.dataSource = self
        picker1.delegate = self
        view.addSubview(picker1)
        
     }
    
    func numberOfComponentsInPickerView(picker1: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(picker1: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return name.count;
    }
    
    func pickerView(picker1: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        label1.text = name[row]
        return name[row]
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


No comments:

Post a Comment