zoukankan      html  css  js  c++  java
  • IOS 拾取器控件

      拾取器是个常用控件。

      在使用拾取器之前需要引用两个协议   <UIPickerViewDataSource,UIPickerViewDelegate>

          还需要连接  datesource & delegate  

      调用协议后,需要重写三个方法

      -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView     这个方法返回拾取器的 个数。

      -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  这个方法返回任意一个拾取器的 数值串

        if(component==0)

          {

              return array_1.count;

          }

          else

          {

              return array_2.count;

          }

          -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component    这个方法返回  任意一个数值串的个数

      

    if(component==0)

        {

            return array_1.count;

        }

        else

        {

            return array_2.count;

        }

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  

    {

        if(component==0)

        {

            NSString *select=[self.array_1 objectAtIndex:row];

            self.array_2=[self.date objectForKey:select];

            [self.picker reloadComponent:1];     //reloadComponet 方法 可以重新刷新 拾取器

        }

    }

    拾取器的数据可以从 p li s t文件里取出

    选择数组 

    选择数组类型。

      NSBundle * bundle=[NSBundle mainBundle];//得到一个bundle;

        NSString * plistPath=[bundle pathForResource:@"List" ofType:@"plist"];//获取路径

        NSDictionary *dic=[[NSDictionary alloc ]initWithContentsOfFile: plistPath]; //通过路径获取到一个字典

        self.date=dic;

        NSArray *array=[self.date allKeys];  //获取所有键

        NSArray *sorted=[array sortedArrayUsingSelector:@selector(compare:)];

        self.array_1=sorted;

        NSString *select_1=[self.array_1 objectAtIndex:0];  //获取第一个字符段

        NSArray *col2=[self.date objectForKey:select_1];

        self.array_2=col2;

     把plist文件里的数据放到一个 字典里。然后通过字典给拾取器赋值。

  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/stuwan/p/4355623.html
Copyright © 2011-2022 走看看