iOS之自定义pickerview(行驶里程数)
1 #pragma mark -- 里程数按钮的点击事件 2 3 - (void)mileageBtnClicked:(UIButton *)sender { 4 5 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"里程数/km\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleAlert]; 6 7 UIPickerView *mileage = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 270, 150)]; 8 9 //指定Delegate 10 11 mileage.delegate = self; 12 13 //显示选中框 14 15 mileage.showsSelectionIndicator = YES; 16 17 [alert.view addSubview:mileage]; 18 19 20 21 UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 22 23 NSInteger row = [mileage selectedRowInComponent:0]; 24 25 NSString *mileageNum = [pickerData objectAtIndex:row]; 26 27 mileageLabel.text = [NSString stringWithFormat:@"%@km", mileageNum]; 28 29 }]; 30 31 32 33 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 34 35 }]; 36 37 38 39 [alert addAction:ok]; 40 41 [alert addAction:cancel]; 42 43 [self presentViewController:alert animated:YES completion:^{ }]; 44 45 46 47 pickerData = [[NSArray alloc] initWithObjects:@"3000",@"3500",@"8000",@"8500",@"13000",@"13500",@"18000",@"18500",@"23000",@"23500",@"28500",@"33000",@"33500",@"38000",@"38500",@"43000",@"43500",@"48000",@"48500",@"53000",@"53500",@"58000",@"58500", nil]; 48 49 } 50 51 #pragma mark -- UIPickerViewDataSource 52 53 //返回显示的列数 54 55 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 56 57 return 1; 58 59 } 60 61 //返回显示的行数 62 63 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 64 65 return pickerData.count; 66 67 } 68 69 #pragma mark -- UIPickerViewDelegate 70 71 //返回当前行的内容 72 73 - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED { 74 75 return [pickerData objectAtIndex:row]; 76 77 } 78 79