Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 20, 2012, 11:52 AM   #1
tmdrbyshr
macrumors newbie
 
Join Date: Aug 2012
How do I code my app to MULTIPLY the output from a DATA PICKER by a TEXT-FIELD ENTRY?

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:

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

    
@interface fruitCost UIViewController

    
<UIPickerViewDataSourceUIPickerViewDelegate>{
    
        
IBOutlet UIPickerView *fruitAndColourPicker;
    
        
NSMutableArray *arrayFruit;
        
NSMutableArray *arrayColour;
    
    }

    @
property (weaknonatomicIBOutlet UILabel *outputLabel;

    @
end 
And here is my .m file:

PHP Code:
    #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;
    
    }

    -(
voidpickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
        
NSInteger fruitRow = [fruitAndColourPicker selectedRowInComponent:0];
        
int FruitRow fruitRow;
    
        
NSInteger colourRow = [fruitAndColourPicker selectedRowInComponent:1];
        
int ColourRow colourRow;
    
        
//APPLES
    
        
if (FruitRow == && ColourRow == 0){[outputLabel setText:[NSString stringWithFormat:@"950"]];}
        else if (
FruitRow == && ColourRow == 1){[outputLabel setText:[NSString stringWithFormat:@"1275"]];}
        else if (
FruitRow == && ColourRow ==2){[outputLabel setText:[NSString stringWithFormat:@"1500"]];}
    
        
//BANANAS
    
        
else if (FruitRow == && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"700"]];}
        else if (
FruitRow == && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat: @"750"]];}
        else if (
FruitRow == && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"800"]];}
    
        
//PEARS
    
        
else if (FruitRow == && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"1050"]];}
        else if (
FruitRow == && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat:@"1150"]];}
        else if (
FruitRow == && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"1250"]];}
        
        
//PEACHES
    
        
else if (FruitRow == && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"850"]];}
        else if (
FruitRow == && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat:@"975"]];}
        else if (
FruitRow == && ColourRow == 2){[outputLabel setText: [NSString stringWithFormat:@"1100"]];}
    
        
//GRAPEFRUIT
    
        
else if (FruitRow == && ColourRow == 0){[outputLabel setText: [NSString stringWithFormat:@"750"]];}
        else if (
FruitRow == && ColourRow == 1){[outputLabel setText: [NSString stringWithFormat:@"825"]];}
        else if (
FruitRow == && 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);
    }

    
//WIDTH OF COMPONENTS

    
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
        if (
component == 0){
            return 
210;
        }
        else return 
90;
    }

    @
end 
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.

Tom
tmdrbyshr is offline   0 Reply With Quote
Old Aug 20, 2012, 01:56 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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!
robbieduncan is offline   0 Reply With Quote
Old Aug 20, 2012, 03:59 PM   #3
larswik
macrumors 65816
 
Join Date: Sep 2006
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.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Field Test code *3001#12345#* nox5 iPhone 3 Dec 10, 2013 02:25 PM
customize text width of picker JPfowl iPhone/iPad Programming 9 Oct 16, 2013 03:23 PM
iPad: Any app for assignments data entry ? soamz iPad 3 Dec 22, 2012 11:51 PM
Resolved: dragging text from table cell to text field straber iPhone/iPad Programming 4 Jul 11, 2012 01:27 PM
Data entry iPad app whooshbong iPad Apps 3 Jun 9, 2012 09:00 AM

Forum Jump

All times are GMT -5. The time now is 04:55 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC