美文网首页
UIPickerview

UIPickerview

作者: 孟维学 | 来源:发表于2016-07-22 11:52 被阅读48次

    #import"ViewController.h"

    @interfaceViewController()

    @property(nonatomic,strong)UIPickerView*pickerView;

    @property(nonatomic,strong)NSArray*pickerViewData;//注意加上这句

    @property(nonatomic,strong)NSArray*pickerViewData1;//注意加上这句

    @property(nonatomic,strong)NSArray*pickerViewData2;//注意加上这句

    @property(nonatomic,strong)UILabel* mylabel;

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    self.mylabel=[[UILabelalloc]initWithFrame:CGRectMake(0,100,self.view.frame.size.width,40)];

    [self.viewaddSubview:self.mylabel];

    self.pickerView=[[UIPickerViewalloc]initWithFrame:CGRectMake(0,200,self.view.frame.size.width,300)];

    self.pickerView.delegate=self;

    self.pickerView.dataSource=self;

    self.pickerView.showsSelectionIndicator=YES;

    [self.viewaddSubview:self.pickerView];

    NSArray*array=[[NSArrayalloc]initWithObjects:@"one",

    @"two",

    @"three",

    @"four",

    @"five",

    nil];

    self.pickerViewData= array;//注意一定要写self,否则不对

    self.pickerViewData1=@[@"每天",@"爱你",@"多",@"一",@"点"];

    self.pickerViewData2=@[@"1",@"2",@"3",@"3",@"4"];

    }

    //组数

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

    {

    return3;

    }

    //每组元素个数

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

    {

    if(component==0) {

    return[_pickerViewDatacount];

    }elseif(component==1){

    return[_pickerViewData1count];

    }

    else{

    return[_pickerViewData2count];

    }

    }

    //每个元素的内容

    -(UIView*)pickerView:(UIPickerView*)pickerView

    titleForRow:(NSInteger)row

    forComponent:(NSInteger)component

    {

    if(component==0) {

    return[_pickerViewDataobjectAtIndex:row];

    }elseif(component==1){

    return[_pickerViewData1objectAtIndex:row];

    }

    else{

    return[_pickerViewData2objectAtIndex:row];

    }

    }

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

    if(component==0) {

    self.mylabel.text=[self.pickerViewDataobjectAtIndex:row];

    NSLog(@"%@",self.mylabel.text);

    }

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    @end

    相关文章

      网友评论

          本文标题:UIPickerview

          本文链接:https://www.haomeiwen.com/subject/mzrijttx.html