-
-
Save geosava/acf861c8c699db64eecd to your computer and use it in GitHub Desktop.
Open UIPickerview or UIDatePicker when user enters textfield
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
//With actionsheet | |
#pragma mark - | |
#pragma mark TextField delegate | |
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{ | |
[myTextField resignFirstResponder]; | |
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; //as we want to display a subview we won't be using the default buttons but rather we're need to create a toolbar to display the buttons on | |
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; | |
CGRect pickerFrame = CGRectMake(0, 40, 0, 0); | |
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame]; | |
pickerView.showsSelectionIndicator = YES; | |
pickerView.dataSource = self; | |
pickerView.delegate = self; | |
[actionSheet addSubview:pickerView]; | |
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; | |
pickerToolbar.barStyle = UIBarStyleBlackOpaque; | |
[pickerToolbar sizeToFit]; | |
NSMutableArray *barItems = [[NSMutableArray alloc] init]; | |
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; | |
[barItems addObject:flexSpace]; | |
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; | |
[barItems addObject:doneBtn]; | |
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)]; | |
[barItems addObject:cancelBtn]; | |
[pickerToolbar setItems:barItems animated:YES]; | |
[actionSheet addSubview:pickerToolbar]; | |
[actionSheet addSubview:pickerView]; | |
[actionSheet showInView:self.view]; | |
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; | |
} | |
//When the pickerview is a UIDatePicker | |
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{ | |
[aTextField resignFirstResponder]; | |
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; | |
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)]; | |
pickerView.datePickerMode = UIDatePickerModeDate; | |
pickerView.hidden = NO; | |
pickerView.date = [NSDate date]; | |
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; | |
pickerToolbar.barStyle = UIBarStyleBlackOpaque; | |
[pickerToolbar sizeToFit]; | |
NSMutableArray *barItems = [[NSMutableArray alloc] init]; | |
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; | |
[barItems addObject:flexSpace]; | |
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; | |
[barItems addObject:doneBtn]; | |
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)]; | |
[barItems addObject:cancelBtn]; | |
[pickerToolbar setItems:barItems animated:YES]; | |
[pickerViewPopup addSubview:pickerToolbar]; | |
[pickerViewPopup addSubview:pickerView]; | |
[pickerViewPopup showInView:self.managingViewController.view]; | |
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)]; | |
} | |
//And to dismiss the action when finished | |
-(void)doneButtonPressed:(id)sender{ | |
//Do something here here with the value selected using [pickerView date] to get that value | |
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; | |
} | |
-(void)cancelButtonPressed:(id)sender{ | |
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; | |
} | |
//Without actionsheet | |
#pragma mark - | |
#pragma mark TextField delegate | |
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{ | |
[myTextField resignFirstResponder]; | |
for (int component = 0; component < (((NSInteger)numberOfComponentsForPickerView) - 1); component++) { | |
NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue]; | |
[pickerView selectRow:valueForRow inComponent:component animated:YES]; | |
} | |
[self fadeInPickerView]; | |
[view addSubview:pickerView]; | |
[pickerView release]; | |
} | |
-(void)fadeInPickerView{ | |
self.pickerView.alpha = 0.0; | |
[UIView beginAnimations:@"" context:nil]; | |
[UIView setAnimationDuration:0.5f]; | |
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; | |
[UIView setAnimationDelegate:self]; | |
self.pickerView.alpha = 1.0; | |
[UIView commitAnimations]; | |
} | |
//see http://www.springtiger.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment