UIIPickerView使用

3060 ワード

//    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:これは私個人が使っているもので、個人は自分のニーズに合わせて実際に関連機能を開発することができます.