zoukankan      html  css  js  c++  java
  • 【实战】如何实现各种拾取器

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

  • 相关阅读:
    创建者模式 -- 单例模式(反射&序列化)
    设计模式(总)
    并不是static final 修饰的变量都是编译期常量
    内部类和静态内部类的加载顺序
    所有的Java虚拟机必须实现在每个类或接口被Java程序 “ 首次主动使用 ” 时才初始化他们
    24 shell 管道命令与过滤器
    26 bash shell中的信号
    25 Linux中的信号
    23 shell 进程替换
    22 shell组命令与子进程
  • 原文地址:https://www.cnblogs.com/ejllen/p/3723228.html
Copyright © 2011-2022 走看看