【实战】如何实现各种拾取器

wps_clip_image-20916

 UIPickerViewDelegate协议必须实现的方法有:

pickerView: titleForRow:forComponent,这个方法根据指定的行号返回该行的标题,也就是向用户显示的字符串。

UIPickerViewDataSource协议必须实现的方法有:

1.numberOfComponentsInPickerView,这个方法返回UIPickerView需要多少个组件。

2.pickerView: numberOfRowsInComponent,这个方法返回指定组件包含多少行。

@interface SinglePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
  
    NSArray    *pickerData;
}
@property (nonatomic,retain) IBOutlet UIPickerView *pickerView;

 

#pragma mark--委托协议方法

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

                  return [pickerData objectAtIndex:row];

}

#pragma mark--数据源协议方法

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 1;

}

-(NSInteger)pickerView:(UIPickerView *)pickView numberOfRowsInComponent:(NSInteger)component{

return [pickerData count];

}

 

-(void)viewDidLoad{

    NSArray   *array = [[NSArray  alloc] initWithObjects:@"欧洲",@"非洲",nil];

}

-(IBAciton)onClickButton:(id)sender{

    NSInteger row = [pickerView selectedRowInComponent:0]; //获得第一列的选择中的行号。

    NSString *selected = [array ObjectAtIndex:row];

}

wps_clip_image-24785

(非关联)

@interface DoublePickerViewController:UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{

         NSArray *array1;

         NSArray *array2;

}

@property (nonatomic,retain) IBOutlet *pickerView;

 

#pragma mark--委托协议方法

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

                  if(component == 0){

                      return [array1 objectAtIndex:row];

                  } else {

                      return [array2 objectAtIndex:row];

                  }

}

#pargam mark--数据源协议方法

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 2;

}   

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger) component{

if(component == 0){

return [array1 count];

} else {

return [array2 count];

}

- (void)viewDidLoad {
    NSArray
*array1 = [[NSArray alloc] initWithObjects:@"欧洲",
                      
@"南美", @"非洲", @"北美",
                      
@"亚洲", @"大洋洲", nil];
    
NSArray *array2 = [[NSArray alloc] initWithObjects:@"足球",
                      
@"篮球", @"羽毛球", @"乒乓球", nil];
    }

-(IBAction)onClick:(id)sender{

    NSInteger row1 = [pickerView selectedRowInComponent:0];

    NSInteger row2 = [pickerView selectedRowInComponent:1];

    NSString *str1 = [array1 ObjectAtIndex:row1];

    NSString *str2 = [array2 ObjectAtIndex:row2];

    NSString *title = [[NSString alloc] initWithFormat:@"你选择了%@的%@项目!",str1,str2];

    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:title message:@"谢谢你的选择."
                                        delegate:nil 
                                        cancelButtonTitle:
@"Ok" 
                                        otherButtonTitles:nil];

    [alert show];

}

 

(关联)

@interface DependentPickerViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{

    NSDictionary *data;
    NSArray
*array1;
    NSArray
*array2;

}

@property (nonatomic,retain) IBOutlet UIPickerView *pickerView;

 

-(void) viewDidLoad{

   //bundle是一个目录,其中包含了程序会使用到的资源.

   NSBundle *bundle = [NSBundle mainBundle];

   NSString *plistPath = [bundle pathForResource:@"足球队dictionary" ofType:@"plist"];

   NSDictionary *data = [[NSDictionary alloc]initWithContentsOfFile:plistPath];

   //这几行代码是从statedictionary.plist属性列表文件中读取到NSDictionary对象中。

  NSArray *col1= [data allkeys];

  NSArray *array1 = [col1 sortedArrayUsingSelector:@selector(compare:)]; //对数据排序。

  NSString *selectCol1 = [array1 objectAtIndex:0];
  NSArray *array2 = [data objectForKey:selectCol1];

}

   #pragma mark--委托协议方法
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
   
if (component == 0) { //选择了第一列
        return [array1 objectAtIndex:row];
    }
else {//选择了第二列
        return [array2 objectAtIndex:row];
    }
}

// 拖动左边的齿轮时,右边的数据相应的Reload更新。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row    //委托方法是实现拾取器控件两个轮互动关键
       inComponent:(NSInteger)component {
   
if (component == 0) {
        NSString
*selectCol1 = [pickerData1 objectAtIndex:row];
        pickerData2
= [data objectForKey:selectCol1]; 
       
[pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView reloadComponent:1];    //重新加载拾取器
    }

 }   

 

    pickerArray = [NSArray arrayWithObjects:@"动物",@"植物",@"石头", nil]; 

    dicPicker = [NSDictionary dictionaryWithObjectsAndKeys: 

    [NSArray arrayWithObjects:@"鱼",@"鸟",@"虫子",nil], @"动物"

    [NSArray arrayWithObjects:@"花",@"草",@"葵花",nil], @"植物"

    [NSArray arrayWithObjects:@"疯狂的石头",@"花岗岩",@"鹅卵石", nil], @"石头",nil]; 

 

posted @ 2014-05-12 13:19  ejllen  阅读(265)  评论(0编辑  收藏  举报