IOS5基础之八------单组件,多组件,依赖组件的选取器

昨天晚上突然断网,原本还要把单组件多组件以及依赖组件放上去。

其实单组件和日期选取器的区别在于单组件的数据来源需要我们提供一个数组,并且还要提供两个协议。

前面基本一致,要声明一个输出口和一个操作方法。

在头文件中可以看到一下变化

#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;

}


发现DidLoad有很大区别。

-(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;

        

}

最后这个方法返回的数字代表每个组件应该具有的宽度。选取器会尽可能的适应这个宽度。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值