昨天晚上突然断网,原本还要把单组件多组件以及依赖组件放上去。
其实单组件和日期选取器的区别在于单组件的数据来源需要我们提供一个数组,并且还要提供两个协议。
前面基本一致,要声明一个输出口和一个操作方法。
在头文件中可以看到一下变化
#import <UIKit/UIKit.h>
@interface BIDSingleComponentPickerViewController : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong,nonatomic) IBOutlet UIPickerView *singlePicker;
@property (strong,nonatomic) NSArray *pickerData;
-(IBAction) buttonPressed;
@end
页面上先在视图属性检查器中设置Button Bar为Tab Bar。分别拖入Picker View 和Button 将Picker选取器的DataSource 和delegate关联到File‘s Owner。Button的Toch UP Inside关联到File’s Owner上buttonPressed。File‘s Owner 关联到Picker选取器的picker。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *array=[[NSArray alloc] initWithObjects:@"Luke",@"Leia",@"Han",@"Chewbacca",@"Artoo",@"Threepio",@"Lando", nil];
self.pickerData=array;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.pickerData=nil;
self.singlePicker=nil;
}
DidLoad在加载的时候生成一个数组作为Picker的数据源。DidUnLoad则是在释放实例。
-(IBAction)buttonPressed
{
NSInteger row =[singlePicker selectedRowInComponent:0];
NSString *selected=[pickerData objectAtIndex:row];
NSString *title=[[NSString alloc] initWithFormat:@"You selected %@",selected];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:title message:@"Thank you for chaoosing" delegate:nil cancelButtonTitle:@"You are welcome" otherButtonTitles:nil];
[alert show];
}
#pragma mark
#pragma mark Picker Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//选取器询问应该显示几个组件
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//给定的组件包含多少行的数据
return [pickerData count];
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//提供关于指定组件中指定行的数据
return [pickerData objectAtIndex:row];
}
#pragma 开头的代码都是一条编译指令。#import <UIKit/UIKit.h>
#define kFillingComponent 0//定义两个常量表示两个组件
#define kBreadComponent 1
@interface BIDDoubleComponentPickerViewController : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong,nonatomic) IBOutlet UIPickerView *doublePicker;
@property (strong,nonatomic) NSArray *fillingTypes;
@property (strong,nonatomic) NSArray *breadTypes;
-(IBAction)buttonPressed;
@end
构建视图和单组件一样。- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *fillingArray=[[NSArray alloc] initWithObjects:@"Ham",@"Turkey",@"Peanut Butter",@"Tuna Salad",@"Chicken Salad",@"Rose Beef",@"Vegemite", nil];
self.fillingTypes=fillingArray;
NSArray *breadArray= [[NSArray alloc] initWithObjects:@"White",@"Whole Wheat",@"Rye",@"Sourdough",@"Seven Grain", nil];
self.breadTypes=breadArray;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.fillingTypes=nil;
self.breadTypes=nil;
self.doublePicker=nil;
}
-(IBAction)buttonPressed
{
NSInteger fillingRow=[doublePicker selectedRowInComponent:kFillingComponent];
NSInteger breadRow=[doublePicker selectedRowInComponent:kBreadComponent];
NSString *bread =[breadTypes objectAtIndex:breadRow];
NSString *filling=[fillingTypes objectAtIndex:fillingRow];
NSString *message=[[NSString alloc] initWithFormat:@"Your %@ on %@ bread will be right up",filling,bread];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Thank you for your order" message:message delegate:nil cancelButtonTitle:@"Great!" otherButtonTitles:nil];
[alert show];
}
这里和单组件类似。#pragma mark
#pragma mark Picker Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component==kFillingComponent)
return [self.fillingTypes count];
else
return [self.breadTypes count];
}
#pragma mark Picker Delegate Methods
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component==kFillingComponent)
return [self.fillingTypes objectAtIndex:row];
else
return [self.breadTypes objectAtIndex:row];
}
这里只是多了判读是那个组件行进行操作。#import <UIKit/UIKit.h>
#define kStateComponent 0
#define kZipComponent 1
@interface BIDDependentComponentPickerViewController : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong,nonatomic) IBOutlet UIPickerView *picker;
@property (strong,nonatomic) NSArray *states;
@property (strong,nonatomic) NSArray *zip;
@property (strong,nonatomic) NSDictionary *stateZips;
-(IBAction)buttonPressed;
@end
其他的没有什么区别。不过这里的数据源用的是外部数据statedictionary.plist。我们把数据源拖入到PIcker项目中就可以了。- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//提取对应用程序的主束的引用
//束只是一种特定的文件夹类型,其中的内容遵循特定的结构。应用程序和框架都是束。
NSBundle *bundle=[NSBundle mainBundle];//主要作用时获取添加到项目的Resource文件夹的资源。
NSURL *plistURL =[bundle URLForResource:@"statedictionary" withExtension:@"plist"];//主束来获取需要的资源路径。
NSDictionary *dictionary=[NSDictionary dictionaryWithContentsOfURL:plistURL];//将创建的内容放到字典中
self.stateZips=dictionary;
//从字典获取所有键的列表,并将这些键分配给states数组。并按字母顺序排序。
NSArray *components=[self.stateZips allKeys];
NSArray *sorted=[components sortedArrayUsingSelector:@selector(compare:)];
self.states=sorted;
//从states数组提取索引为0的对象。
NSString *selectedState=[self.states objectAtIndex:0];
NSArray *array=[stateZips objectForKey:selectedState];
self.zip=array;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.picker=nil;
self.states=nil;
self.zip=nil;
self.stateZips=nil;
}
-(IBAction)buttonPressed
{
NSInteger stateRow=[picker selectedRowInComponent:kStateComponent];
NSInteger zipRow=[picker selectedRowInComponent:kZipComponent];
NSString *state=[self.states objectAtIndex:stateRow];
NSString *zip1=[self.zip objectAtIndex:zipRow];
NSString *title= [[NSString alloc]initWithFormat:@"You selected zip code %@",zip1];
NSString *message=[[NSString alloc]initWithFormat:@"%@ is in %@",zip1,state];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
#pragma mark
#pragma mark Picker Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component ==kStateComponent)
return [self.states count];
return [self.zip count];
}
#pragma mark Picker Delegate Methods
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component==kStateComponent)
return [self.states objectAtIndex:row];
return [self.zip objectAtIndex:row];
}
//只要选择器的选择发生变化都会调用这个方法
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component==kStateComponent)
{
NSString *selectedState=[self.states objectAtIndex:row];
NSArray *array=[stateZips objectForKey:selectedState];
self.zip=array;
[picker selectRow:0 inComponent:kZipComponent animated:YES];
[picker reloadComponent:kZipComponent];
}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if(component==kZipComponent)
return 90;
return 200;
}
最后这个方法返回的数字代表每个组件应该具有的宽度。选取器会尽可能的适应这个宽度。