Skip to main content

How to uipickerview in swift

This lesson I want to show you how to create uipicker view in ios


 And then create  a new project with CommandShift-N project
fill in your name project:
This is work place:
After that you click here Main.story.board
we have to drag a uipickerview UIimageview and lableview on to View Controller:

Open the Assistant editor and control-drag from the picker view and make an outlet named myPickerView
control-drag from the UIImageview and make an outlet named myImageview

control-drag from the Lableview  and make an outlet named myLableview

Close Assitant  editor and go to the ViewController.swift file .Clean up the code then addthe data for the "Uipickerview" like this

UIPickerView needs a delegate and a data source.  In the viewDidLoad above we set both the delegate and dataSource to to self so we can add the required methods here. Change the class declaration to this:


Creating the Data

in line 13, we’re  declaring a new  Array instance  variable to store the list of data.By declaring it here as an instance variable. We can access this variable form any method in this class  and variable will hold its value for the duration of the object lifetime.
Error:you have to add some required methods for the protocol the two methods are in the data source .add these toward the bottom of your code :

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return color.count
    }
We have only one component for the picker view so we return a literal 1. Using .count we get the number of rows from the data. We have some optional methods to use in UIPickerViewDelegate. Add these below the Data Source methods:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return color[row]
    }

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        myLable.text = color[row]

}

Detecting UIPickerView selected Row

First step is we have to add the datasource array as like below.
    var uiColor = [UIColor.white,UIColor.red,UIColor.blue,UIColor.green,UIColor.gray]
we have to drag some picture on to project:
 
we have to add the datasource array as like below.
    var imageview = ["g1.jpg","g2.jpg","g3.jpg","g4.jpg","g5.jpg"]

Note: g1 g2....it is name of picture

When selected UIPickerView row the below delegate method will be called, here we have changed the background color of uiview corresponding to the UIPickerView selected row values.

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        myLable.text = color[row]

        myImageview.image = UIImage(named: imageview[row])


        self.view.backgroundColor = uicolor[row]


Then, Build and Run the application you will get the output like below.


Comments

kevin pham said…
hey everyone it's Kien but you can call me Kevin i hope that this Blog help you learn a lot
please post any question and comment right here so that i can support for you

Popular posts from this blog

When is viewWillAppear, viewDidAppear, viewDidLoad, viewWillDisappear actually called

A few months ago I decided to apply for a job at a software company, I interviewed for IOS developer position. After a week I got a result they  gave me an offer 400$ per months, salary not too high, not too low for me  I also  struggled with a lot of questions from my interviewer, so today in this article I will share with you my experiment to answer the question from the interviewer  if you are  IOS developer you are familiar with  viewWillAppear , viewDidAppear , viewDidLoad , viewWillDisappear , and viewDidDisappear  let me ask you a question what is the running order of viewDidAppear , viewDidLoad , viewWillDisappear , and viewDidDisappear. This question sounds easy it’s straightforward. Actually, it very hard to answer, because when you make app maybe you don't care about it , No one tells you before you get an interview ,  now we need to create a new project, inside of ViewController.swift  we need to implement some method...

Build Carousel Effect in iOS with Swift Kien Pham

This tutorial I’m going to show you how to build Carousel effect it is the most common, you can see it everywhere you can see it on Instagram or App Store which is beautiful UI you can use a lot of in your app many places  that you want to show beautiful text and beautiful image view First thing first we need to create a new project and select iOS->SingleView Application Product Name: Tableview – this the name of your project ·      Company Identifier: KienPhamDev It’s actually the domain name written the other way round. if  you have a domain, you can use your own domain name otherwise you may use mine or just fill in ·      Device family: you can choose universal it’s not important ·      Choose swift as Language And then we are going to the main storyboard , in this tutorial we want to use ViewController so we just need to drag a new Collection View from Object library ...

How to update two images using GCD in swift

If you are IOS developer you can run into a problem when your app download a lot of image from URL maybe your app is crashed or you can't interact with the rest of the application, when I interviewed for a job  a few months ago, I also got this question from my interviewer   He assumed that there are 3 images from URL when user click on the update button all of them will be updated at  the same time so let’s do it a demo to know what happens when your application run without multithreading    Inside of my project I need  some element into ViewController,  As you can see we have 2 images and a button to update our image  Inside of ViewController , we need to hook up all of them with  @IBOutlet and @IBAction it should look like this  As you can see when user click on the update button to download two images from URL, you can't interact with the rest of the application you can’t d...