May 25, 2013

UIPickerView. "Straight and to the point" tutorial.


This tutorial is a source of creating UIPickerView and filling it with data from an array of values.
1. Drop Picker View from Object Panel(right bottom side) on your view.

2. In your .h file, declare Picker View, Array(you will fill it with values for Picker View), String(variable which you would use for sting a value chosen from pricker view). 
Also, create properties for those variables. Do not forget to set UIPickerViewDelegate and UIPickerViewDataSource.
3.   Now, synthesize all properties in .m file, right after @implementation.
4. Go to storyboard and connect your View Controller with UIPickerView by dragging from View Controller to the picker.
5. Set DataSource, Delegate with your View Controller by dragging from from right panel to View Controller. At the moment of dragging, picker view should be selected. After all manipulations, your window should look like this:



6. Now, go to .m file and start coding. Fill array in ViewDidLoad method. The values of array will be shown in picker view.

7. The most important part is do net forget to write all  4 requiered methods for Picker Views:
method which counts rows in picker view:
 In our situation, we have an array, that is why we are counting objects of an array. In common cases, you can simply set number: return 10;

method which counts components in picker view:
method which assigns each value of the array to a proper row of a picker:
method which assigns selected item to the variable:
That is it. Run your program.You should get similar to the following:


No comments:

Post a Comment