-
-
Save jiamaozheng/6113b4f1aba706ec5621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface TestTwoController : UIViewController <UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment