//加载pickerview在viewDidLoad函数中调用该函数即可,刚开始将pickerview放在屏幕最底方,通过调用下面的showPickerView函数使其从底部动态出现
-(void) addPickerView {
if (pickerView ==nil) {
pickerView = [[UIPickerViewalloc] initWithFrame:CGRectMake(0,460, 320, 460)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;//选中某行时会和其他行显示不同
[self.viewaddSubview:pickerView];
[pickerViewrelease];
}
}
//使pickerview从底部出现
-(void) showPickerView {
[UIViewbeginAnimations: @"Animation"context:nil];//设置动画
[UIViewsetAnimationDuration:0.3];
pickerView.frame =CGRectMake(0,240, 320, 460);
[UIViewcommitAnimations];
}
//使pickerview隐藏到屏幕底部
-(void) hidePickerView {
[UIViewbeginAnimations:@"Animation"context:nil];
[UIViewsetAnimationDuration:0.3];
pickerView.frame =CGRectMake(0,460, 320, 460);
[UIViewcommitAnimations];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
//返回每个组件上的行数
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return2;
}
//设置每行显示的内容
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (row ==0) {
return@"男";
}else {
return@"女";
}
}
//自定义pickerview使内容显示在每行的中间,默认显示在每行的左边((NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label = [[[UILabelalloc] initWithFrame:CGRectMake(0.0f,0.0f, [pickerViewrowSizeForComponent:component].width, [pickerViewrowSizeForComponent:component].height)]autorelease];
if (row ==0) {
label.text =@"男";
}else {
label.text =@"女";
}
[labelsetTextAlignment:UITextAlignmentCenter];
return label;
}
//当你选中pickerview的某行时会调用该函数。
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
/NSLog(@"You select row %d",row);
if (row ==0) {
selectLabel.text = @"you select 男";
}elseif (row == 1) {
selectLabel.text = @"you select 女";
}*/
}
//设置指定组件上每行的宽度
/*
-(void)pickerView:(UIPickerView *)thePickerView widthForComponet:component {
}
*/