I found an interesting issues with the iPhone SDK and the UIPickerView
The didSelectRow method gets called after a users selects a new value on a picker wheel. The method knows what wheel (component) was spun and the index of the newly selected value. This works great for a picker with a single wheel. My picker had 5 wheels in it.
Long story short, I didn’t catch this problem when I submitted my app, “Ka-Ching”, to the iTunes store. As a result of not catching this, Apple rejected my app. I could argue that the iPhone SDK should have so programmatic hooks that would only allow one component to be spun at a time, or even a method to return the state of all of components in the picker.
Trying to figure out a solution to the problem I expanded on my idea of recording the state of all the components in the picker when the didSelectRow method was called. Realistically, by the time that didSelectRow gets processed it is possible that all 5 of the components had changed.
Looking through the SDK documentation for the UIPickerView I came across this:
- (NSInteger)selectedRowInComponent:(NSInteger)component
With this method you can determine the selected row index for any of the components in your picker. you don’t have to rely on the values passed into the didSelectRow method.
THE WRONG WAY TO DO IT
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
//get the value of the component that was changed
if(component == 0)
{
currency_value = [currency objectAtIndex:row];
}
else if(component == 1)
{
hundereds_value = [hundereds objectAtIndex:row];
}
else if(component == 2)
{
tens_value = [tens objectAtIndex:row];
}
else if(component == 3)
{
ones_value = [ones objectAtIndex:row];
}
else if(component == 4)
{
change_value = [cents objectAtIndex:row];
}
//some code here to save the values
}
THE RIGHT WAY TO DO IT
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
//get the values
NSInteger currency_NewRow = [thePickerView selectedRowInComponent:0];
NSInteger hundereds_NewRow = [thePickerView selectedRowInComponent:1];
NSInteger tens_NewRow = [thePickerView selectedRowInComponent:2];
NSInteger ones_NewRow = [thePickerView selectedRowInComponent:3];
NSInteger cents_NewRow = [thePickerView selectedRowInComponent:4];
//some code to save the values
}
- 大小: 45.1 KB
分享到:
相关推荐
IOS移动手机中的导航条类(UIPickerView),官方文档,不骗人,英文的。
UIPickerView的使用(基础版)Demo,UIPickerView的使用(基础版)Demo
UIPickerView 简单使用,此例只是实现简单的UIPickerView功能,可以别的功能组合,效果更好!
iOS:用UIPickerView实现UIDatePicker效果。用UIPickerView实现UIDatePicker效果
水平版本的 UIPickerView CPPickerView ,CPPickerView 是一个可定制的水平版本的 UIPickerView (基于滚轮)
ios应用源码之自定义风格uipickerview .
UIPickerView Demo 选择器控件源代码
ActionSheetPicker = UIPickerView + UIActionSheet
UIPickerView DEMO UIPickerView DEMO UIPickerView DEMO
IOS应用源码——UIPickerView选择多个选项效果.zip
UIPickerView的使用(实现省市县),UIPickerView的使用(实现省市县)
IOS自定义弹出UIPickerView或UIDatePicker(动画效果)
ios UIPickerView封装的中国省/市/区地址选择器 ,含有中国最新城市plist文件
http://blog.csdn.net/duxinfeng2010/article/category/1155790 有详细讲解
UIPickerView加一个时间范围如:(00:00-01:00)的选择项
一个选择城市的PickerView,自定义封装View,在工程里可调用。
这是根据全国的省 市 区来做的一个用UIPickerView实现的三级联动
UIPickView、UIPickView基本用法、UIPickView详解,UIPickView常用方法
UIPickerView自定义选择器,包括字体、颜色、大小选择