码迷,mamicode.com
首页 > 其他好文 > 详细

UIPickerView 使用

时间:2015-09-29 11:41:26      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

//初始化 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:这是我个人使用时用的,个人可根据自己的需求实际开发相关功能。



UIPickerView 使用

标签:

原文地址:http://my.oschina.net/jack088/blog/512157

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!