View Full Version : How do I code my app to MULTIPLY the output from a DATA PICKER by a TEXT-FIELD ENTRY?

Aug 20, 2012, 11:52 AM
So far, my app consists of a Data Picker with two components and a Label that displays an integer based on the combination of these two components.

I now want to add a text field (preferably with a Number Pad Keyboard) that allows a user to type in another integer.

I would then like the integer based on the output of the Data Picker and the integer typed into the Text Field to be multiplied and the product of this calculation to be shown in another Label.

For simplicity's sake I have changed the names of the arrays to FRUIT and COLOUR. Let's say that in this instance, the Text Field is used to input quantity and the final input will be cost.

I have not changed the figures produced by the Data Picker so don't expect them to make any sense within the context of fruit and colour.

Okay. Here is my .h file:

#import <UIKit/UIKit.h>
#define FRUIT 0
#define COLOUR 1

@interface fruitCost : UIViewController

<UIPickerViewDataSource, UIPickerViewDelegate>{

IBOutlet UIPickerView *fruitAndColourPicker;

NSMutableArray *arrayFruit;
NSMutableArray *arrayColour;


@property (weak, nonatomic) IBOutlet UILabel *outputLabel;


And here is my .m file:

#import "fruitCost.h"

@implementation fruitCost
@synthesize outputLabel;

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 2;


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent (NSInteger)component{

if (component == FRUIT)
return [arrayFruit count];

if (component == COLOUR)
return [arrayColour count];

return 0;


-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

if (component == FRUIT)
return [arrayFruit objectAtIndex:row];

if (component == COLOUR)
return [arrayColour objectAtIndex:row];

return 0;


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

NSInteger fruitRow = [fruitAndColourPicker selectedRowInComponent:0];
int FruitRow = fruitRow;

NSInteger colourRow = [fruitAndColourPicker selectedRowInComponent:1];
int ColourRow = colourRow;


if (FruitRow == 0 && ColourRow == 0){[outputLabel setText:[NSString stringWithFormat:@"950"]];}
else if (FruitRow == 0 && ColourRow == 1){[outputLabel setText:[NSString stringWithFormat:@"1275"]];}
else if (FruitRow == 0 && ColourRow ==2){[outputLabel setText:[NSString stringWithFormat:@"1500"]];}


else if (FruitRow == 1 && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"700"]];}
else if (FruitRow == 1 && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat: @"750"]];}
else if (FruitRow == 1 && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"800"]];}


else if (FruitRow == 2 && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"1050"]];}
else if (FruitRow == 2 && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat:@"1150"]];}
else if (FruitRow == 2 && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"1250"]];}


else if (FruitRow == 3 && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"850"]];}
else if (FruitRow == 3 && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat:@"975"]];}
else if (FruitRow == 3 && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"1100"]];}


else if (FruitRow == 4 && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"750"]];}
else if (FruitRow == 4 && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat:@"825"]];}
else if (FruitRow == 4 && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"900"]];}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

// Custom initialization
return self;

- (void)viewDidLoad
[super viewDidLoad];

arrayFruit = [[NSMutableArray alloc]init];
[arrayFruit addObject:@"Apples"];
[arrayFruit addObject:@"Bananas"];
[arrayFruit addObject:@"Pears"];
[arrayFruit addObject:@"Peaches"];
[arrayFruit addObject:@"Grapefruit"];

arrayColour = [[NSMutableArray alloc]init];
[arrayColour addObject:@"RedLow"];
[arrayColour addObject:@"Yellow"];
[arrayColour addObject:@"Green"];


- (void)viewDidUnload
[self setOutputLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);


-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

if (component == 0){
return 210;
else return 90;


I am new to Xcode so if any answers I receive could be as clear as possible, that would be great. I realise this is a lot of code and quite a long post but I hope that the solution to my problem is not too complex to trouble anyone with.

Any help would be much appreciated.

Thank you.


Aug 20, 2012, 01:56 PM
Sorry if I've missed it but I don't see a description of the problem. What, exactly, does not work. Are there any error messages? Output? If so what is the output and what was expected? Posting the code is great but don't leave us guessing!

Aug 20, 2012, 03:59 PM
Sounds like you want to enter a value into a textfield then take the value from your data picker and multiply them together. Just convert the strings to integer value and apply some good old basic C (which you should know) to do that math and convert back to a string to display the data again.

There is a Method out there that I used not to long ago that allows you to do calculations without having to convert to primitive data types first but I can't remember what it was, but it exists I just don't remember what Class it was connected to.