Skip to content

Instantly share code, notes, and snippets.

@ktuite
Last active February 17, 2016 10:29
Show Gist options
  • Save ktuite/fc23de2f129458630f18 to your computer and use it in GitHub Desktop.
Save ktuite/fc23de2f129458630f18 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface TestTwoController : UIViewController <UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@end
#import "TestTwoController.h"
@interface TestTwoController ()
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) NSArray *pickerArray;
@property (strong, nonatomic) IBOutlet UIPickerView *picker2;
@property (strong, nonatomic) NSArray *picker2Array;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@end
@implementation TestTwoController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.pickerArray = [[NSArray alloc] initWithObjects:@"female", @"male", @"it's complicated", nil];
self.picker2Array = [[NSArray alloc] initWithObjects:@"13-15", @"16-19", @"20-29", @"30-39", @"40-49", @"50-59", @"60-69", @"70-79", @"80-89", @"90-99", @"100-110", @"over 110", nil];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.picker2 = [[UIPickerView alloc] initWithFrame:CGRectZero];
[self attachPickerToTextField:self.textField :self.picker];
[self attachPickerToTextField:self.textField2 :self.picker2];
}
- (void)attachPickerToTextField: (UITextField*) textField :(UIPickerView*) picker{
picker.delegate = self;
picker.dataSource = self;
textField.delegate = self;
textField.inputView = picker;
}
#pragma mark - Keyboard delegate stuff
// let tapping on the background (off the input field) close the thing
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.textField resignFirstResponder];
[self.textField2 resignFirstResponder];
}
#pragma mark - Picker delegate stuff
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == self.picker){
return self.pickerArray.count;
}
else if (pickerView == self.picker2){
return self.picker2Array.count;
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == self.picker){
return [self.pickerArray objectAtIndex:row];
}
else if (pickerView == self.picker2){
return [self.picker2Array objectAtIndex:row];
}
return @"???";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == self.picker){
self.textField.text = [self.pickerArray objectAtIndex:row];
}
else if (pickerView == self.picker2){
self.textField2.text = [self.picker2Array objectAtIndex:row];
}
}
@end
@saadmirza
Copy link

Slick and Simple, that's all what i can say about it. i ve been trying so many complicated solutions and none of them worked as charm as yours. thanx a million

@pnovales
Copy link

Super simple. Great job. Thanks Kathleen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment