`
cenphoenix
  • 浏览: 160512 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

UIPickerView – spinning multiple components

阅读更多

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
分享到:
评论

相关推荐

    UIPickerView

    func numberOfComponents(in pickerView: UIPickerView) -> Int { return 2 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return dishes[component]....

    UIPickerView小示例

    func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 // 如果只有一个列,返回1,如果有多个列,返回列数 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent ...

    代码实现UIPickerView

    - `numberOfComponents(in:)`:返回`UIPickerView`包含的组件数量。一般情况下,`UIPickerView`只有一个组件,但也可以有多个。 - `pickerView(_:numberOfRowsInComponent:)`:返回指定组件中的行数,即选择项的...

    UIPickerView Demo

    4. **实现DataSource方法**:`numberOfComponents(in:)`返回选择器的组件数量,通常是1。`pickerView(_:numberOfRowsInComponent:)`返回指定组件中的行数,这取决于你要展示的数据量。 5. **实现Delegate方法**:`...

    UIPickerView的使用(基础版)Demo

    在iOS开发中,UIPickerView是一个非常常见的组件,用于展示一系列可滚动的选择项,用户可以通过滚动选择他们感兴趣的项目。这个“UIPickerView的使用(基础版)Demo”旨在帮助开发者理解如何在Objective-C (OC)环境中...

    自定义 UIPickerView

    func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 // 如果只有一个组件 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return data...

    UIPickerView 城市两级选择

    `UIPickerView` 的核心数据源方法是 `numberOfComponents(in:)` 和 `pickerView(_:titleForRow:forComponent:)`,前者返回PickerView的列数,后者返回特定行的标题。 在实现城市选择的功能时,我们需要两层`...

    ios应用源码之自定义风格uipickerview 20181210

    在iOS开发中,UIPickerView 是一个非常常用的控件,用于展示用户可以选择的多个选项,类似于下拉列表。本教程将深入探讨如何自定义风格的UIPickerView,以提升用户体验和应用界面的独特性。我们将围绕以下几点进行...

    用UIPickerView实现UIDatePicker效果

    在iOS开发中,UIPickerView和UIDatePicker是两种常见的用户界面组件,它们各自有着不同的应用场景。本篇文章将深入探讨如何使用UIPickerView来模拟实现UIDatePicker的效果,以满足特定的设计需求或优化性能。 首先...

    UIPickerView多选

    在多选场景下,你需要重写`numberOfComponents(in:)`返回至少两个组件(一个用于显示选项,另一个用于显示选中状态),`pickerView(_:numberOfRowsInComponent:)`返回每个组件的行数,以及`pickerView(_:titleForRow...

    水平版本的 UIPickerView CPPickerView.zip

    水平版本的 UIPickerView CPPickerView ,CPPickerView 是一个可定制的水平版本的 UIPickerView (基于滚轮)

    UIPickerView加一个时间范围的demo

    UIPickerView由一系列列(components)组成,每一列可以显示多个行(rows)。在这个案例中,我们可能只需要一列,因为时间通常由小时和分钟两部分构成。每个部分都可以通过UIPickerView的代理方法来设置和处理。 1....

    UIPickerView的使用(实现省市县)

    - `numberOfComponents(in:)`:返回 picker view 的列数。 - `pickerView(_:numberOfRowsInComponent:)`:返回指定列中的行数。 - `pickerView(_:titleForRow:forComponent:)`:返回指定行的标题。 - `...

    IOS应用源码——UIPickerView选择多个选项效果.zip

    在iOS开发中,UIPickerView是一个非常常见的组件,它用于展示一系列可滚动的行,用户可以从这些行中选择一个或多个选项。这个压缩包“IOS应用源码——UIPickerView选择多个选项效果.zip”显然是一个关于如何实现...

    UIPickerView+UIActionSheet

    在iOS开发中,UIPickerView和UIActionSheet是两种常见的用户界面组件,它们分别用于不同的交互场景。`UIPickerView`通常用于展示一系列可滚动的选择项,比如日期选择、时间选择或者一组分类选项。而`UIActionSheet`...

    UIPickerView、UIPickerView基本用法、UIPickerView详解

    ### UIPickerView详解及基本用法 #### 一、概述 `UIPickerView` 是iOS系统提供的一种用户界面组件,主要用于展示可滚动的选择器视图。它能够有效地帮助用户从多个选项中选择所需的数据,并且可以配置成多列的形式...

    IOS自定义弹出UIPickerView或UIDatePicker(动画效果)

    在iOS开发中,UIPickerView和UIDatePicker是两种常用的组件,用于用户选择数据或设置日期。本教程将深入探讨如何在iOS应用中自定义这两种控件,并添加动画效果,以提升用户体验。以下是对这个主题的详细讲解: 首先...

    ios UIPickerView封装的中国省/市/区地址选择器 ,含有中国最新城市plist文件

    在iOS开发中,`UIPickerView` 是一个常用的控件,用于展示一系列可滚动的选项。在这个特定的项目中,开发者已经对`UIPickerView`进行了封装,使其成为一个功能完善的中国省、市、区地址选择器。这个选择器不仅包含了...

    ios-时间选择器 (UIPickerView).zip

    在iOS开发中,UIPickerView是一个非常常用的组件,它用于展示一系列可滚动的选项供用户选择。本资源“ios-时间选择器 (UIPickerView).zip”显然关注于利用UIPickerView来创建一个时间选择器功能,使得用户可以方便地...

Global site tag (gtag.js) - Google Analytics