obj-c UIPickerView 使用


头文件:


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>


@property (nonatomic,strong)UIPickerView *pickkView;


@property(nonatomic,strong) NSArray* firstArray;


@property(nonatomic,strong)NSArray* endArray;


@end



m文件


#import "ViewController.h"


@interface ViewController ()




@end

@implementation ViewController


@synthesize pickkView;

@synthesize firstArray;

@synthesize endArray;


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.firstArray=[[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

    

    //设置窗体背景颜色

    self.view.backgroundColor=[UIColor blackColor];

    //初始化控件

    self.pickkView=[[UIPickerView alloc]init];

    //设置控件的背景颜色

    self.pickkView.backgroundColor=[UIColor whiteColor];

    //设置控件剧中

    self.pickkView.center=self.view.center;

    //设置控件数据

    self.pickkView.dataSource=self;

    self.pickkView.delegate = self;

    //设置是否有水平选择效果

    self.pickkView.showsSelectionIndicator=YES;

    //把控件添加到窗体上

    [self.view addSubview:self.pickkView];


    

    // Do any additional setup after loading the view, typically from a nib.

}

//这个方法主要用来设定控件到底有多少列

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    NSInteger intLength=0;

    if([pickkView isEqual:self.pickkView])

    {

        intLength=2;

    }

    return intLength;

}


//这个方法主要用来设置每一列有多少行

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

{

    NSInteger intLength=0;

    if([pickkView isEqual:self.pickkView])

    { 

        //如果是第一个行 则返回多少列

        if(component==0)

        {

            intLength=[firstArray count];

        }

        //其他行返回多少列

        else

        {

            intLength=[endArray count]//count 方法是返回数组的长度;

        }

    }

    return intLength;

}

//绑定显示的数据 或者叫设置每一列的每一行 到底显示什么

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row

            forComponent:(NSInteger)component{


    NSString *result = nil;

    if ([pickerView isEqual:self.pickkView])

    {

        if(component==0)

        {

            result = [self.firstArray objectAtIndex:row];

        }

        else

        {

            result=[self.endArray objectAtIndex:row];

        }

    }

    return result;

}


//当你选择某一项的时候会触发这个事件

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

{

    if(component ==0)//如果响应的是第一个滚轮,则处理下面的动作

    {

        NSString * selectStr=[self.firstArray objectAtIndex:row];

        self.endArray=[[NSArray alloc]initWithObjects:[selectStr stringByAppendingString:@"0"],[selectStr stringByAppendingString:@"1"],[selectStr stringByAppendingString:@"2"],[selectStr stringByAppendingString:@"3"],[selectStr stringByAppendingString:@"4"],[selectStr stringByAppendingString:@"5"],[selectStr stringByAppendingString:@"6"],[selectStr stringByAppendingString:@"7"],[selectStr stringByAppendingString:@"8"],[selectStr stringByAppendingString:@"9"],[selectStr stringByAppendingString:@"10"],nil];

        

        //更具选择的项进行更新

        [self.pickkView selectRow:0 inComponent:1 animated:NO];

        [self.pickkView reloadComponent:1];

    }

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值