Alright So i am experimenting with uipickerviews to learn more about Xcode and develop a simple app. I have everything the way i want it except one thing the outputs. The way i have it setup is it will provide the output using label. I can get it to give an output but i need it to give an output based on the selection and i am now stuck at a dead end here.
the app converts quantities like grams to oz, pound to gram, etc. I want to simple make it so its like this: in component 1 you select grams. then in component 2 you select pounds. and the output would be: " 1 pound is equal to 453 grams" and it would display this text on the output label I have the label linked and setup so it can give outputs its jut the actual code i cant figure out without messing up.
here is my .h:
and my .m
i tired many different ways but i cant seem to get it. sorry for the long post i just want to be as clear as possible of what I'm trying to accomplish.
Thanks in advance
the app converts quantities like grams to oz, pound to gram, etc. I want to simple make it so its like this: in component 1 you select grams. then in component 2 you select pounds. and the output would be: " 1 pound is equal to 453 grams" and it would display this text on the output label I have the label linked and setup so it can give outputs its jut the actual code i cant figure out without messing up.
here is my .h:
Code:
#import <UIKit/UIKit.h>
#define kFillingComponent 0
#define kBreadComponent 1
@interface FirstViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>{
IBOutlet UIPickerView *doublePicker;
NSArray *fillingTypes;
NSArray *breadTypes;
}
@property (nonatomic,retain) UIPickerView *doublePicker;
@property (nonatomic,retain) NSArray *fillingTypes;
@property (nonatomic,retain) NSArray *breadTypes;
@property (nonatomic,retain) IBOutlet UILabel *OutputLabel;
@end
and my .m
Code:
#import "FirstViewController.h"
#import "NewViewController.h"
@implementation FirstViewController
@synthesize doublePicker;
@synthesize fillingTypes;
@synthesize breadTypes;
@synthesize OutputLabel;
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSArray *breadArray = [[NSArray alloc] initWithObjects:
@"1 Gram",@"1/8 Ounce",@"1/4 Ounce",@"1/2 Ounce",@"1 Ounce",@"1/4 Pound",@"1 Pound",nil];
self.breadTypes = breadArray;
[breadArray release];
NSArray *fillingArray = [[NSArray alloc] initWithObjects:
@"1Gram",@"1/8 Ounce",@"1/4 Ounce",@"1/2 Ounce",@"1 Ounce",@"1/4 Pound",@"1 Pound",nil];
self.fillingTypes = fillingArray;
[fillingArray release];
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[self setOutputLabel:nil];
}
- (void)dealloc {
[doublePicker release];
[breadTypes release];
[fillingTypes release];
[super dealloc];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
if (component == kBreadComponent)
return[self.breadTypes count];
return[self.fillingTypes count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == kBreadComponent)
return [self.breadTypes objectAtIndex:row];
return [self.fillingTypes objectAtIndex:row];
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *breadTypesSelected = [breadTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *fillingTypesSelected = [fillingTypes objectAtIndex:[pickerView selectedRowInComponent:1]];
if(breadTypesSelected == 0 && fillingTypesSelected == 0){[OutputLabel setText:[NSString stringWithFormat:@"test"]];}
else if(breadTypesSelected == 0 && fillingTypesSelected == 0){[OutputLabel setText:[NSString stringWithFormat:@"test2"]];}
}
@end
i tired many different ways but i cant seem to get it. sorry for the long post i just want to be as clear as possible of what I'm trying to accomplish.
Thanks in advance
Last edited: