Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 14, 2015 06:59
Show Gist options
  • Select an option

  • Save lamprosg/5047276 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5047276 to your computer and use it in GitHub Desktop.
(OS) Simple Picker
//So the datasource and delegate will be this view controller.
//So drag and drop the picker outlets in the .xib to the File's Owner (this class)
//Necessary Protocols
//OR drag and drop the datasource and delegate from the connection inspector to the File's Owner
@interface BIDViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) IBOutlet UILabel *resultLabel;
@property (strong, nonatomic) IBOutlet UITextField *dollarText;
@property (strong, nonatomic) NSArray *countryNames;
@property (strong, nonatomic) NSArray *exchangeRates;
//Action to dismiss the keyboard when hitting return (didEndOnExit signal)
- (IBAction)editingEnded:(id)sender;
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Initialize the arrays
self.countryNames = [[NSArray alloc] initWithObjects:
@"Australia (AUD)", @"China (CNY)", @"France (EUR)",
@"Great Britain (GBP)", @"Japan (JPY)", nil];
self.exchangeRates = [[NSArray alloc]
initWithObjects: [NSNumber numberWithFloat:0.9922],
[NSNumber numberWithFloat:6.5938],
[NSNumber numberWithFloat:0.7270],
[NSNumber numberWithFloat:0.6206],
[NSNumber numberWithFloat:81.57], nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.resultLabel = nil;
self.dollarText = nil;
self.picker = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)editingEnded:(id)sender
{
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource - UIPickerViewDataSource protocol
//Called by the PickerView to identify the number of components
//How many wheels
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
//Number of rows in a wheel
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [self.countryNames count];
}
//string to be displayed for a specified row in a specific component.
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [self.countryNames objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate - UIPickerViewDelegate protocol
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
//Get the exchange rate based on the row selected by the user
float rate = [[self.exchangeRates objectAtIndex:row] floatValue];
//Get the amount of dollars the user typed inthe text field
float dollars = [self.dollarText.text floatValue];
//Calculate the result
float result = dollars * rate;
NSString *resultString = [[NSString alloc] initWithFormat:@"%.2f USD = %.2f %@", dollars, result,
[self.countryNames objectAtIndex:row]];
//show the result to the label
self.resultLabel.text = resultString;
}
Before starting it is worth taking some time to talk about the delegate and datasource of the UIPickerView class.
In order to obtain the options to be displayed to the user, the PickerView needs a data source.
This data source takes the form of a protocol that defines the methods that must be implemented in order to provide
the Picker with data information. At a minimum the class designated as the data source must implement the following methods:
numberOfComponentsInPickerView: - Called by the PickerView to identify the number of components
(i.e. selection wheels) that are to be displayed to the user.
numberOfRowsInComponent: - Informs the PickerView of the number of rows (in other words the selection options)
that are present in a specified component.
titleForRow: - Called by the PickerView to identify the string that is to be displayed for a specified row
in a specific component.
The PickerView also needs a mechanism for notifying the application code when a selection has been made by the user.
It achieves this by calling the
didSelectRow method
of the class declared as the PickerView’s delegate.
In order to fully implement a PickerView, therefore, it is necessary for the object to be assigned a data source and delegate.
Typically the view controller responsible for the PickerView is the best place to implement these two protocols.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment