How to update label with uipickerview value
Above video is updated for X code 8.2.1 & Swift 3.
Use pickerview as input view to the textfield.
So in this video we are going to learn how to update the label from a picker view. If you want to download the x code project file for this project then just fill out the form below and you will receive project in your inbox. If you are looking to use picker view as input view to the textfield then I have completely separate blog post on that. Check out the first link in the description box.
Alright so let’s just dive right into it. So I am going to create new X code project. iOS single view application next. I am going to call it label from picker view. And them I am going to save this on the desktop. So first I am going to main storyboard. Change the iPhone size to SE. And add a label in here. Make it little bigger. Centre it.
Set the alignment to Centre. Then I am going to add picker view and set it at the bottom of this view controller. Then I am going to open the assistant editor. Create an outlet for picker label. Then I am also going to create an outlet for picker view. So I am going to close the assistant editor.
Alright so you need to connect the delegate and the data source for this picker view to the view controller. And there are two ways you can do that. So if you right click or control click on this then you can just connect the data source and you can connect the delegate. I am going to cancel these connections.
So you can do this through storyboard or I am going to do this through programatically. So I am going to set Picker view dot data source is equal to self. And picker view dot delegate is equal to self. Alright so this is giving error so I am going to add the delegate. So UIPickerViewDataSource and UIPickerViewDelegate.
Add these as subclass to the view controller. Now this is again giving me an error. Because there are some required methods that I have not used it yet. So the first required method is Number of components in picker view. I am going to return one. Because there is single component.
Then Second requirement is number of rows in component. Before I write that I am going to create Picker options. So let pick options is equal to. Let’s just say I am going to pick colours. Red, maybe green, then maybe yellow and blue. So now I am going to write number of rows in components is pick options dot count.
So that will get rid of all the errors. Then I am going to set title for each row. Is going to be return pick options at index row. So the title for each row is going to be these colours. Then once you select the particular item then I am going to set did select row. So picker view did select row.
In that case picker label dot text is equal to pick options at index row. And that’s it. And if I run this app in iPhone 5s. Alright so as I choose a different color. label is automatically updated. Alright so if you haven’t already then you can download this project file by just filling out the form below.
And you will receive x code file in your inbox. Again if you are looking for picker view as input view to the text field. Then I have separate blog post on it. Check out the first link in the description box. So that’s it for this video. Leave a comment, like this video & I will see you in the next one.