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:
And here is my .m file:
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
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:
#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;
@end
And here is my .m file:
PHP:
#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;
//APPLES
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"]];}
//BANANAS
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"]];}
//PEARS
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"]];}
//PEACHES
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"]];}
//GRAPEFRUIT
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);
}
//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