UIPickerView 使用

//初始化 pickerView
-(void)initPickerView{
    pickerArray = [NSArray arrayWithObjects:@"顺丰",@"申通",@"韵达",@"圆通",@"天天",@"全峰",@"中通",@"百世汇通", nil];
    logisticsPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 568, 320, 180)];
    logisticsPicker.delegate = self; //绑定事件
    logisticsPicker.dataSource = self; //绑定数据元
    [logisticsPicker setBackgroundColor:[UIColor whiteColor]]; 
    [self.view addSubview:logisticsPicker];
    [self.view bringSubviewToFront:logisticsPicker];//将视图放置最前面
}
//每行显示的个数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
//显示多少行
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [pickerArray count];
}
//每行显示的标题名称
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerArray objectAtIndex:row];
}

//点击空白隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    [self inView];
}
//设置隐藏动画
- (void)popView {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];//动画时间长度,单位秒,浮点数
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    self.logisticsPicker.frame = CGRectMake(0, 400, 320, 180);
    
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
//    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
}
//设置弹出动画
- (void)inView {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];//动画时间长度,单位秒,浮点数
    self.logisticsPicker.frame = CGRectMake(0, 568, 320, 180);
    
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
//    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
}
-(void)animationFinished{
    NSLog(@"动画结束!");
}
//绑定 页面点击事件
-(IBAction)logisticsPickerBtn:(id)sender{
    if(lpEntity){
        [self.view endEditing:YES];
        [self popView];
    }else{
        chvc = [[CalendarHomeViewController alloc]init];
        chvc.calendartitle = @"出行日期";
        [chvc setAirPlaneToDay:365 ToDateforString:nil];//飞机初始化方法
        chvc.calendarblock = ^(CalendarDayModel *model){
            logisticsName.text = [model toString];
        };
        [self.navigationController pushViewController:chvc animated:YES];
    }
}
//当你选中 pickerview 的某行时会调用该函数。
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"You select row %ld",(long)row);
    logisticsName.text = [pickerArray objectAtIndex:row];
}

ps:这是我个人使用时用的,个人可根据自己的需求实际开发相关功能。



转载于:https://my.oschina.net/jack088/blog/512157

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值