1

I have a UIPickerView display with the inputView of a textField. I have split the pickerView into two columns. One is whole numbers 50-500. The other is 0.25,0.50,0.75. I want them to be able to select the whole number, and then the decimal part of the number with the other column. I can't figure out how to get to display the "full" number. When i move the second column, it just registers it as moving the column one and changes the whole number. I want the user to be able to select a whole number, the decimal part of the number, hit done and the textField display a number with format "100.25" or whatever number they select. I have the done button coded...and the whole number displays with just format i.e "100".

Thanks!

UPDATE!!! CODE!!!!

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component{
if ([pickerView isEqual:weightPickerView]) {
    weightField.text = [NSString stringWithFormat:@"%d.%d",[weightPickerArray   objectAtIndex:row], [weightPickerArray2 objectAtIndex:row]];



-(void)populateWeightPickerArray{
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight <=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
    [weightPickerArray addObject:weightString];
}
}

-(void)populateWeightPickerArray2{
weightPickerArray2 = [[NSMutableArray alloc] init];
[weightPickerArray2 addObject:@"0.25"];
[weightPickerArray2 addObject:@"0.50"];
[weightPickerArray2 addObject:@"0.75"];
}

So I want the user to select their weight-- for example 100 with column one and 0.25 in column two. Then I want the text field to display 100.25...

UPDATE 2! changed code, and now I receive an error..

[Session started at 2012-08-03 10:39:39 -0500.] 2012-08-03 10:39:45.656 CalorieAppFinal[1088:207] -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b336c0 2012-08-03 10:39:45.661 CalorieAppFinal[1088:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b336c0' * Call stack at first throw

here is how I initialize my weightPicker

 -(IBAction)weightFieldDidBeginEditing{
weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

if ([pickerView isEqual:weightPickerView]) {
    int valOne = [[weightPickerArray objectAtIndex:row] intValue];
    CGFloat valTwo = [[weightPickerArray2 objectAtIndex:row] floatValue];
    weightField.text = [NSString stringWithFormat:@"%d.%.2f",valOne, valTwo];
}


-(void)populateWeightPickerArray{
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight <=500; weight++) {
    [weightPickerArray addObject:[NSNumber numberWithInt:weight]];
}
 }

-(void)populateWeightPickerArray2{
weightPickerArray2 = [[NSMutableArray alloc] init];
[weightPickerArray2 addObject:[NSNumber numberWithFloat:0.25f]];
[weightPickerArray2 addObject:[NSNumber numberWithFloat:0.50f]];
[weightPickerArray2 addObject:[NSNumber numberWithFloat:0.75f]];
 }
user1450221
  • 109
  • 2
  • 12
  • How is your weightPickerView initialised? – Rick Aug 03 '12 at 17:00
  • -(IBAction)weightFieldDidBeginEditing{ weightPickerView = [[UIPickerView alloc] init]; weightField.inputView = weightPickerView; weightPickerView.showsSelectionIndicator = YES; weightPickerView.delegate = self; weightPickerView.dataSource = self; [weightPickerView release]; } @Rick – user1450221 Aug 03 '12 at 18:02
  • Seems like you released weightPickerView in -(IBAction)weightFieldDidBeginEditing and attempt to access it later in "if([pickerView isEqual:weightPickerView])". Release weightPickerView only after you are done using it (after removing the pickerview from the view). – Rick Aug 04 '12 at 02:42

3 Answers3

1

**Edited as first answer was a tad confusing

When displaying a float or decimal number in a string you have two common approaches.

stringWithFormat
stringByAppendingFormat

So if you have two columns/pickers triggering two separate methods, or if you are triggering the same method but checking for a specific picker you could do this:

- (void)somePickerDidChangeValueMethod:(UIPicker)picker
{
    //If you have on float value
    myTextField.text = [NSString stringWithFormat:@"%.2f",yourFloatValue];
            //the %.xf represents how many decimal places to show
    //If you have two int values
    myTextField.text = [NSString stringWithFormat:@"%d.%d",int1,int2];


    //If incoming number is a float
    myTextField.text = [myTextField.text stringByAppendingFormat:@".%.0f",yourFloatValue];
    //If incoming number is an int
    myTextField.text = [myTextField.text stringByAppendingFormat:@".%d",intVal];
}

Hopefully this helps.

fatuous.logic
  • 750
  • 1
  • 5
  • 16
  • I am using the pickerView didSelectRow method, and then textField.text = "(code)". I tried you versions with the didSelectRow method, and I got really weird numbers in the output. I will share the code that I have of the pickerView, and for displaying the output. Will you please look at it and see if you can help me again? I will update question with code... @vitality – user1450221 Aug 02 '12 at 00:33
1

Try this:

-(void)populateWeightPickerArray2
{
    weightPickerArray2 = [[NSMutableArray alloc] init];
    [weightPickerArray2 addObject:@".25"];  /* remove the leading zero */
    [weightPickerArray2 addObject:@".50"];  /* remove the leading zero */
    [weightPickerArray2 addObject:@".75"];  /* remove the leading zero */
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([pickerView isEqual:weightPickerView]) 
    {
        /* Updated the following line with selectedRowinComponent */
        weightField.text = [NSString stringWithFormat:@"%@%@",[weightPickerArray objectAtIndex:[weightPickerView selectedRowInComponent:0]], [weightPickerArray2 objectAtIndex:[weightPickerView selectedRowInComponent:1]]];
    }
}

To use the number for calculation:

float number = [weightField.text floatValue];

If you want to display "0.25", "0.50" and "0.75", you will have to perform some string manipulations to remove the leading zero before appending the decimal segment to the integer segment. See this post

Community
  • 1
  • 1
Rick
  • 1,818
  • 1
  • 15
  • 18
  • If you want to use the number for math calculations, simply use: [weightField.text floatValue] anywhere in your code. – Rick Aug 03 '12 at 16:52
  • i used the code you gave me above, for pickerView didselect row, but it doesnt work completely right on displaying the correct number. For example, with the picker I have 51 selected in the first row, and .50 selected in the second row and the textField displays 51.50, then if i only move the right column(decimals) to .75 it changes the textField to 52.75 instead of what it should be(51.75). – user1450221 Aug 03 '12 at 18:18
  • I see. That was because of the used of the same "row" variable in weightField.text = [NSString stringWithFormat:@"%@%@",[weightPickerArray objectAtIndex:row], [weightPickerArray2 objectAtIndex:row]]. You will need to use different variables to remember the row for first column and another variable for second column. – Rick Aug 04 '12 at 02:15
0

You could use the answer below this, however, if you were planning to use the values in some math later on it's probably easier in the long run to use NSNumber's. Up to you - both would work.

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([pickerView isEqual:weightPickerView])
    {
        int valOne = [[weightPickerArray objectAtIndex:row] intValue];
        CGFloat valTwo = [[weightPickerArray2 objectAtIndex:row] floatValue];
        weightField.text = [NSString stringWithFormat:@"%d.%.2f",valOne,valTwo];
    }
}


-(void)populateWeightPickerArray
{
    weightPickerArray = [[NSMutableArray alloc] init];
    for (int weight = 50; weight <=500; weight++)
    {
        //easier to store NSNumberWithInt as opposed to a string
        [weightPickerArray addObject:[NSNumber numberWithInt:weight]];
    }
}

-(void)populateWeightPickerArray2
{
    weightPickerArray2 = [[NSMutableArray alloc] init];
    [weightPickerArray2 addObject:[NSNumber numberWithFloat:0.25f]];
    [weightPickerArray2 addObject:[NSNumber numberWithFloat:0.50f]];
    [weightPickerArray2 addObject:[NSNumber numberWithFloat:0.75f]];
}

Try something like that.

Hope it helps.

fatuous.logic
  • 750
  • 1
  • 5
  • 16
  • Took your advice and used the NSNumber, however, now when i run the app and click on the weightField, my app crashes. Error will be posted in question. And yes, I will be using the numbers in some math calculations later. @vitality. – user1450221 Aug 03 '12 at 15:42