头文件:
#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.
}