UIPickerView 标签栏与选取器(2)

来源:互联网 发布:php中文网视频下载 编辑:程序博客网 时间:2024/06/02 02:16

一个又好了,下面进行第三个,doublePicker。打开doubleView.xib,向里面拖入一个UIPickerView和一个Button,还需要调整其大小为411,这5个view都需要调整其大小,因为状态栏和标签栏占去了一些空间。

开始定义变量和方法了,打开doublePicker.h,修改代码为:

#import <UIKit/UIKit.h>
#define first 0//第一个滚轮
#define second 1//第二个滚轮

@interface doublePicker : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
{
 IBOutlet UIPickerView *picker;
 NSArray *firstArray;//给第一个滚轮提供数据
 NSArray *secondArray;//给第二个滚轮提供数据
}
@property (nonatomic, retain) UIPickerView *picker;
@property (nonatomic, retain) NSArray *firstArray;
@property (nonatomic, retain) NSArray *secondArray;

-(IBAction)click;
@end

关联和singleView一样,注意dataSource和delegate的关联。

然后修改doublePicker.m为:
#import "doublePicker.h"


@implementation doublePicker
@synthesize picker;
@synthesize firstArray;
@synthesize secondArray;
-(IBAction)click
{
 NSInteger firstRow = [picker selectedRowInComponent:first];//得到第一个滚轮的行
 NSInteger secondRow = [picker selectedRowInComponent:second];//得到第二个滚轮的行
 
 NSString *firstString = [firstArray objectAtIndex:firstRow];//得到第一个滚轮当前显示的值
 NSString *secondString = [secondArray objectAtIndex:secondRow];//得到第二个滚轮当前显示的值
 
 NSString *message = [[NSString alloc]initWithFormat:@"第一个是%@,第二个是%@!",firstString,secondString];//格式化字符串
 
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Picket" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alert show];
 [alert release];
 [message release];
}

-(void)viewDidLoad
{
 //初始化第一个滚轮的值
 NSArray *arrayFirst = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",nil];
 self.firstArray = arrayFirst;
 [arrayFirst release];
 
 //初始化第二个滚轮的值
 NSArray *arraySecond = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",nil];
 self.secondArray = arraySecond;
 [arraySecond release];
}

……
- (void)dealloc {
 [picker release];
 [firstArray release];
 [secondArray release];
 [super dealloc];
}

#pragma mark -
#pragma mark picker data source methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
 return 2;//返回两个滚轮
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
 if(component == first)//根据选定的滚轮返回相应的行数
  return [self.firstArray count];
 else {
  return [self.secondArray count];
 }

}

#pragma mark picker delegate methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 if(component == first)//根据选定的滚轮返回当前行的值
 {
  return [self.firstArray objectAtIndex:row];
 }
 else {
  return [self.secondArray objectAtIndex:row];
 }

}

@end

然后看结果:两类数据,都可以进行选择。单击按钮给出picker上面显示的提示。

 

下面实现独立组件,也就是键值的关联,例如省份,省份下面有市,市下面有县一样,这里实现两个关联,书本上例子实现的是地区和邮政编码,我这里方便说明问题随便弄了几个数据,也就是1对应从11~19,2对应从21~29以此类推。首先建立一个plist文件吧。在Resources上右击,Add-new file-other-Property list然后进行一些编辑就好了。(这个其实就是xml文件,用其它的编辑软件弄好导入也可以的)。建好后打开dependentView.xib,向里面拖入一个UIPickerView和一个button,大小改为411.

声明变量:
#import <UIKit/UIKit.h>
#define first 0
#define second 1

@interface dependentPicker : UIViewController {
 IBOutlet UIPickerView *picker;
 NSArray *firstArray;
 NSArray *secondArray;
 NSDictionary *dictionary;
}
@property (nonatomic, retain) NSArray *firstArray;
@property (nonatomic, retain) NSArray *secondArray;
@property (nonatomic, retain) NSDictionary *dictionary;
@property (nonatomic, retain) UIPickerView *picker;

-(IBAction)click;
@end

同上面的进行关联。然后修改代码:

#import "dependentPicker.h"


@implementation dependentPicker

@synthesize picker;
@synthesize firstArray;
@synthesize secondArray;
@synthesize dictionary;
-(IBAction)click//同singlePicker,给出提示
{
 NSInteger firstRow = [picker selectedRowInComponent:first];
 NSInteger secondRow = [picker selectedRowInComponent:second];
 
 NSString *firstString = [firstArray objectAtIndex:firstRow];
 NSString *secondString = [secondArray objectAtIndex:secondRow];
 
 NSString *message = [[NSString alloc]initWithFormat:@"第一个是%@,第二个是%@!",firstString,secondString];
 
 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Picket" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alert show];
 [alert release];
 [message release];
 
}

-(void)viewDidLoad//加载是初始化picker的值
{
 NSBundle *bundle = [NSBundle mainBundle];
 NSString *plistPath = [bundle pathForResource:@"datalist" ofType:@"plist"];
 
 NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:plistPath];//初始化一个dictionary保存plist中的值
 self.dictionary = dic;
 [dic release];
 
 NSArray *array = [self.dictionary allKeys];//把dictionary中的所以值放入数组array中
 NSArray *shortArray = [array sortedArrayUsingSelector:@selector(compare:)];//选出第一个滚轮中的值
 self.firstArray = shortArray;
 
 NSString *selectArray = [self.firstArray objectAtIndex:0];//根据第一个滚轮中的值,选择第二个滚轮中的值
 NSArray *secondShortArray = [dictionary objectForKey:selectArray];
 self.secondArray = secondShortArray;
}
……
- (void)dealloc {
    [super dealloc];
}
#pragma mark -
#pragma mark picker data source methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
 return 2;//返回两个滚轮
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
 if(component == first)
  return [self.firstArray count];
 else {
  return [self.secondArray count];
 }
 
}
#pragma mark picker delegate methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 if(component == first)
 {
  return [self.firstArray objectAtIndex:row];
 }
 else {
  return [self.secondArray objectAtIndex:row];
 }
 
}

//只要选取器发生变化就会触发该事件,进行关联,具体就是当你更改第一个滚轮总的值是,第二个滚轮会自动更新
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 if(component == first)//如果响应的是第一个滚轮,则处理下面的动作
 {
  NSString *selectState = [self.firstArray objectAtIndex:row];//得到第一个滚轮当前的行
  NSArray *array = [dictionary objectForKey:selectState];//根据第一个滚轮当前值从dictionary中选择对应的第二个滚轮中的值
  self.secondArray = array;
  [picker selectRow:0 inComponent:second animated:NO];
  [picker reloadComponent:second];//重新装载第二个滚轮中的值
 }
 
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component//使用委托设置Picker的大小
{
 if(component == second)//如果是第二个滚轮,就设置大小为90
  return 90;
 return 200;//否则设置为200
}

@end

运行结果: