zoukankan      html  css  js  c++  java
  • iphone pickerview 两列例子

    1. #import
    2. //定义两个常量,用来表示2个组件
    3. #define PickerOne 0
    4. #define PickerTwo 1
    5. //添加UIPickerView的委托方法和数据源方法
    6. @interface DoublePickerViewViewController : UIViewController
    7. {
    8.         //定义输出口,字典,数组
    9.         UIPickerView *picker;
    10.         NSDictionary *zidian;
    11.         NSArray *city;
    12.         NSArray *zips;
    13. }
    14. @property (nonatomic ,retain) IBOutlet UIPickerView *picker;
    15. @property (nonatomic ,retain) NSDictionary *zidian;
    16. @property (nonatomic ,retain) NSArray *city;
    17. @property (nonatomic ,retain) NSArray *zips;
    18. //定义按钮点击时候触发的代码
    19. - (IBAction) buttonPressed:(id)sender;
    20. @end
    复制代码


    .m文件

    1. #import "DoublePickerViewViewController.h"
    2. @implementation DoublePickerViewViewController
    3. @synthesize picker;
    4. @synthesize zidian;
    5. @synthesize city;
    6. @synthesize zips;
    7. //按钮触发代码
    8. - (IBAction) buttonPressed:(id)sender
    9. {
    10.         //询问选取器选中了哪个行
    11.         NSInteger cityRow = [picker selectedRowInComponent:PickerOne];
    12.         NSInteger zipsRow = [picker selectedRowInComponent:PickerTwo];
    13.         //返回选中行的值
    14.         NSString *cityName = [self.city objectAtIndex:cityRow];
    15.         NSString *zipsName = [self.zips objectAtIndex:zipsRow];
    16.         //把选中的值分配给字符串
    17.         NSString *title = [[NSString alloc] initWithFormat:@"你选择的邮编是%@",zipsName];
    18.         NSString *message = [[NSString alloc] initWithFormat:@"%@是在%@里的邮编",
    19.                                               zipsName,cityName];
    20.         //定义一个警告窗口
    21.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
    22.                                                 cancelButtonTitle:@"OK" otherButtonTitles:nil];
    23.         [alert show];
    24.         [alert release];
    25.         [title release];
    26.         [message release];
    27. }
    28. - (void)viewDidLoad
    29. {
    30.         //提取对应用程序主束的引用,主要是为了获取添加在Resources的资源
    31.         NSBundle *bundle = [NSBundle mainBundle];
    32.         //获取code.plist的路径
    33.         NSString *listPath = [bundle pathForResource:@"code" ofType:@"plist"];
    34.         //用获取到的路径创建一个字典文件
    35.         NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:listPath];
    36.         //分配给zidian
    37.         self.zidian = dictionary;
    38.         [dictionary release];
    39.         //获取字典里所有的数组
    40.         NSArray *components = [self.zidian allKeys];
    41.         //按照字母顺序排序
    42.         NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
    43.         //然后分配给city
    44.         self.city = sorted;
    45.         //提取索引为0的city行,也就是第一行
    46.         NSString *selectedCity = [self.city objectAtIndex:0];
    47.         //用这个返回的字符串提取对应的zips数组
    48.         NSArray *array = [zidian objectForKey:selectedCity];
    49.         //将它分配给zips
    50.         self.zips = array;
    51.     [super viewDidLoad];
    52. }
    53. - (void)viewDidUnload
    54. {
    55.         self.picker = nil;
    56.         self.zidian = nil;
    57.         self.city = nil;
    58.         self.zips = nil;
    59.         [super viewDidUnload];
    60. }
    61. - (void)dealloc {
    62.         [picker release];
    63.         [zidian release];
    64.         [city release];
    65.         [zips release];
    66.     [super dealloc];
    67. }
    68. #pragma mark -
    69. #pragma mark picker 数据源方法
    70. //选取器如果有多个滚轮,就返回滚轮的数量,我们这里有两个,就返回2
    71. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    72. {
    73.         return 2;
    74. }
    75. //返回给定的组件有多少行数据,我们有2个组件,所以用if操作。后期tableview也是类似的操作方法
    76. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    77. {
    78.         if (component == PickerOne) {
    79.                 return [self.city count];
    80.         }
    81.         return [self.zips count];
    82. }
    83. #pragma mark -
    84. #pragma mark picker 委托方法
    85. //官方的意思是,指定组件中的指定数据,我理解的就是目前选中的是哪一行。
    86. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
    87. forComponent:(NSInteger)component
    88. {
    89.         if (component == PickerOne) {
    90.                 return [self.city objectAtIndex:row];
    91.         }
    92.         return [self.zips objectAtIndex:row];
    93. }
    94. //当选取器的行发生改变的时候调用这个方法
    95. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
    96. inComponent:(NSInteger)component
    97. {
    98.         //如果第一个滚轮发生改变,开始一下方法。
    99.         if (component == PickerOne) {
    100.                 //选中了哪个城市
    101.                 NSString *selectedCity = [self.city objectAtIndex:row];
    102.                 //在字典里找到对应的邮编
    103.                 NSArray *array = [zidian objectForKey:selectedCity];
    104.                 //分配给zips
    105.                 self.zips = array;
    106.                 //右侧的邮编,默认停留在第一行,0表示第一行。
    107.                 [picker selectRow:0 inComponent:PickerTwo animated:YES];
    108.                 //重新加载第二个滚轮。
    109.                 [picker reloadComponent:PickerTwo];
    110.         }
    111. }
    112. @end
  • 相关阅读:
    方法参数个数最多不宜超过4个
    避免方法中使用大量局部变量
    JQuery学习备忘
    CSS学习备忘
    解析Path方法备忘
    获取差集合的一种实现思路
    前台JSP页面独立化
    requireJs的使用
    handlebar
    移动端h5<a>标签点击样式去除
  • 原文地址:https://www.cnblogs.com/rollrock/p/2576963.html
Copyright © 2011-2022 走看看