Custom pickerview for iOS (mileage) and iospickerview
# Pragma mark-Click Event of the mileage button
-(Void) mileageBtnClicked :( UIButton *) sender {
UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "mileage/km \ n" message: nil preferredStyle: UIAlertControllerStyleAlert];
UIPickerView * mileage = [[UIPickerView alloc] initWithFrame: CGRectMake (0, 40,270,150)];
// Specify the Delegate
Mileage. delegate = self;
// Display the selected box
Mileage. showsSelectionIndicator = YES;
[Alert. view addSubview: mileage];
UIAlertAction * OK = [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action ){
NSInteger row = [mileage selectedRowInComponent: 0];
NSString * mileageNum = [pickerData objectAtIndex: row];
MileageLabel. text = [NSString stringWithFormat: @ "% @ km", mileageNum];
}];
UIAlertAction * cancel = [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action ){
}];
[Alert addAction: OK];
[Alert addAction: cancel];
[Self presentViewController: alert animated: YES completion: ^ {}];
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];
}
# Pragma mark -- UIPickerViewDataSource
// Returns the number of columns displayed.
-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {
Return 1;
}
// Returns the number of lines displayed.
-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {
Return pickerData. count;
}
# Pragma mark -- UIPickerViewDelegate
// Return the content of the current row
-(Nullable NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component _ TVOS_PROHIBITED {
Return [pickerData objectAtIndex: row];
}