I've been trying to get into iPhone development so I've been working with some pretty basic stuff...I'm trying to practice using multi-component pickers by making an app that allows you to pick a bread and meat for a sandwich.
Everything works in it except when I run it in the iPhone simulator, instead of listing the choices in the picker (ie. wheat, white, ham, turkey) it only shows question marks. However, when you select two of the question marks it notifies you of which sandwich you have chosen.
Does anyone know what my error is that is causing this to happen? Any input will be greatly appreciated.
Here's my code if this helps:
along with:
Again, thank you so much for any help!
Everything works in it except when I run it in the iPhone simulator, instead of listing the choices in the picker (ie. wheat, white, ham, turkey) it only shows question marks. However, when you select two of the question marks it notifies you of which sandwich you have chosen.
Does anyone know what my error is that is causing this to happen? Any input will be greatly appreciated.
Here's my code if this helps:
Code:
// DoubleComponentPickerViewController.h
#import <UIKit/UIKit.h>
#define kFillingComponent 0
#define kBreakComponent 1
@interface DoubleComponentPickerViewController : 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;
-(IBAction)buttonPressed;
@end
along with:
Code:
// DoubleComponentPickerViewController.m
#import "DoubleComponentPickerViewController.h"
@implementation DoubleComponentPickerViewController
@synthesize doublePicker;
@synthesize fillingTypes;
@synthesize breadTypes;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
-(IBAction)buttonPressed
{
NSInteger breadRow = [doublePicker selectedRowInComponent:kBreakComponent];
NSInteger fillingRow = [doublePicker selectedRowInComponent:kFillingComponent];
NSString *bread = [breadTypes objectAtIndex:breadRow];
NSString *filling = [fillingTypes objectAtIndex:fillingRow];
NSString *message = [[NSString alloc] initWithFormat:@"Your %@ on %@ will be right up", filling, bread];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
@"Thank you for your order"
message:message
delegate:nil
cancelButtonTitle:@"Great!"
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}
- (void)viewDidLoad {
NSArray *breadArray = [[NSArray alloc] initWithObjects:@"White", @"Whole Wheat", @"Rye", @"Sourdough", @"Seven Grain", nil];
self.breadTypes = breadArray;
[breadArray release];
NSArray *fillingArray = [[NSArray alloc] initWithObjects:@"Ham", @"Turkey", @"Peanut Butter", @"Tuna Salad", @"Chicken Salad", @"Roast Beef", @"Vegemite", nil];
self.fillingTypes = fillingArray;
[fillingArray release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[doublePicker release];
[breadTypes release];
[fillingTypes release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
if (component == kBreakComponent)
return [self.breadTypes count];
return [self.fillingTypes count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forCompopnent:(NSInteger)component
{
if (component == kBreakComponent)
return [self.breadTypes objectAtIndex:row];
return [self.fillingTypes objectAtIndex:row];
}
@end
Again, thank you so much for any help!