UIPickerView with DONE button - iphone

I have a UIPickerView in my application, and I want to have a done button on it to hide the picker.
I don't want to use an action sheet because I want focus on the rest of the view.i mean all the view has to be active.
Here is how I create the picker:
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,210 , 320, 15);
[self.view addSubview:pickerViewCountry];
[pickerViewCountry release];

try out the custom picker make with action sheet
UIActionSheet *actionSheet;
NSString *pickerType;
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *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];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}

You can add a toolbar to a UIPickerView like this:
// initiaize picker view
UIPickerView *pickerView=[[UIDatePicker alloc]init];//Date picker
pickerView.frame=CGRectMake(0,44,320, 216);
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setMinuteInterval:5];
[self.view addsubview:pickerView]
toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeDateFromLabel:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[pickerView addSubview:toolBar];

The picker view should be set as the inputView of the text field where you are entering the country. You should create a toolbar with a standard Done system item and set that as the inputAccessoryView of the same field.

You can do this by adding a UIToolBar with Done button with animation of UIPickerView.

I would even try using something like EAActionSheetPicker. It manages the display of a pickerView/datePicker inside of an actionSheet. It may just be what you're looking for.

Related

Two pickers for 2 textfield in one view

I have 2 text fields. Each has own picker view.When a user clicks on each of them, UIPickerView appears. Everything works. However, when,first, I click on the second text field, picker view for the second text field appears, and first text field takes a result from this picker view, not the second one. Something is not right in my code. I cannot figure out what exactly. Please help...Thanks in advance)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == reminder){
choosen = reminderString;
mySheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[mySheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
myArray = [[NSMutableArray alloc]initWithObjects: #"15 minutes",#"20 minutes",#"30 minutes", #"45 minutes",nil];
CGRect pickerFrame = CGRectMake(0,44,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate =self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
[mySheet addSubview:pickerView];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mySheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spaser = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"set" style:UIBarButtonItemStyleDone target:self action:#selector(DatePickerDoneClick)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelRepetition)];
[controlToolBar setItems:[NSArray arrayWithObjects:spaser, setButton, cancelButton,nil] animated:NO];
[mySheet addSubview:controlToolBar];
[mySheet showInView:self.view];
[mySheet setBounds:CGRectMake(0, 0, 320, 485)];
return NO;
}
else if(textField == repetition){
choosen = repetitionString;
repetitionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[mySheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
myArray = [[NSMutableArray alloc] initWithObjects:#"One-time task",#"Daily",#"Weekly",#"Yearly",nil];
CGRect pickerFrame = CGRectMake(0,44,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate =self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
[mySheet addSubview:pickerView];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mySheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spaser = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:#"set" style:UIBarButtonItemStyleDone target:self action:#selector(DatePickerDoneClick)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelRepetition)];
[controlToolBar setItems:[NSArray arrayWithObjects:spaser, setButton, cancelButton,nil] animated:NO];
[mySheet addSubview:controlToolBar];
[mySheet showInView:self.view];
[mySheet setBounds:CGRectMake(0, 0, 320, 485)];
return NO;
}
else{
return 0;
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return myArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [myArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (chosen == reminderString) {
reminderString =[myArray objectAtIndex:row];
[reminder setText:reminderString];
}
else if (chosen == repetitionString){
reminderString =[myArray objectAtIndex:row];
[repetition setText:reminderString];
}
}
- (void)DatePickerDoneClick{
[mySheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancelRepetition{
[mySheet dismissWithClickedButtonIndex:0 animated:YES];
}
you can give the name to pickers and then distinguish between them for
ex Picker1 and picker2 and in didselectrow put the condition
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(pickerview == picker1)
{
reminderString =[myArray objectAtIndex:row];
[reminder setText:reminderString];
}
elseif(pickerview == picker2)
{
reminderString =[myArray objectAtIndex:row];
[repetition setText:reminderString];
}

when picker view selected keyboard still visible

i have a text field and picker view.when i select the text field picker view is appearing and if i select row in picker still my textfield is editable. i want to set textfield only in drop down list.and one more problem my key board is visible when i select the textfield
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[txtstate resignFirstResponder];//this is to hide key board
txtstate.inputView=pickerView;
ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(43,337, 212, 160)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(43, 0, 212, 30)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[ViewForValuePicker addSubview:toolBar];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(43, 30, 212, 140);
pickerView.delegate = self;
pickerView.dataSource= self;
pickerView.showsSelectionIndicator = YES;
[ViewForValuePicker addSubview:pickerView];
errstate.hidden=YES;
[testScroll addSubview:ViewForValuePicker];
[pickerView setHidden:NO];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height)] ;
}
retval.text = [[arr objectAtIndex:row]objectForKey:#"Code"];
retval.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
return retval;
}
- (void)doneBtnPressToGetValue{
[pickerView resignFirstResponder];
[pickerView removeFromSuperview];
txtcity.text=nil;
autocompleteTableView.hidden=YES;
[ ViewForValuePicker removeFromSuperview];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
txtLId.text=[[arr objectAtIndex:row] valueForKey:#"LId"];
txtstate.text= [[arr objectAtIndex:row] valueForKey:#"Code"];
//txtstate.userInteractionEnabled=NO;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arr count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [[arr objectAtIndex:row] valueForKey:#"Code"];
}
Hello try and update following code with your code or check full code at http://pastie.org/6569798 :-
First define
UIActionSheet *actionSheet;
in header file(.h).
- (void)createActionSheet {
if (actionSheet == nil)
{
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIToolbar *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];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField==txtstate)
{
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 320, 420);
pickerView.delegate = self;
pickerView.dataSource= self;
pickerView.showsSelectionIndicator = YES;
[pickerView setHidden:NO];
[self createActionSheet];
[actionSheet addSubview:pickerView];
[textField resignFirstResponder];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *retval = (id)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height)] ;
}
retval.text = [arr objectAtIndex:row];
retval.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
return retval;
}
- (void)doneBtnPressToGetValue
{
[txtstate resignFirstResponder];
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
Assign your textfield.inputview in viewDidLoad itself with pickerView.
In textFieldDidBeginEditing - no more code required.
Also use your UIPickerViewDelegate methods as it is.
If you want to have just the pickerview show up when you select the text field, take all of the code you have right now in textFieldDidBeginEditing and move it into viewDidLoad. If you initialize txtstate.inputView=pickerView; before hand it will replace the keyboard as the main input.

How to display DatePicker in popupwindow in iPhone?

I m newbie to objective-C
I want to display the UIDatePicker in a popup window after the button click ..
I have a button and when I click the button my popup should appear with DatePicker and later after chosing the date the popup should close and set the selected date in a textbox.
How can I do this ?
To create a datepicker I wrote this code ..
UIDatePicker *datePicker=[[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];
But I do not know how to display it in a popup window on a button click ..?
Declaration in your .h file
UIActionSheet *aac;
UIDatePicker *theDatePicker;
Implementation in .m file
// Add the code after your comment
-(void)DatePickerDoneClick:(id)sender {
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:theDatePicker.date]] componentsSeparatedByString:#""];
[dateString1 release];
dateString1=nil;
dateString1 = [[NSString alloc]initWithString:[temp objectAtIndex:0]];
UITextField* BirthDayTxtLBl.text = [NSString stringWithFormat:#" %#",dateString1];
NSString *theTime = [NSString stringWithFormat:#"%#",BirthDayTxtLBl.text];
NSLog(#"%#",theTime);
[aac dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)DatePickercancelClick:(id)sender{
[aac dismissWithClickedButtonIndex:0 animated:YES];
}
-(IBAction)AddTheTimePicker:(id)sendar {
aac = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?#"\n\n":nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
theDatePicker.datePickerMode=UIDatePickerModeDateAndTime;
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick:)];
//doneBtn.tag = tagID;
[barItems addObject:doneBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UILabel *toolBarItemlabel;
if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
toolBarItemlabel= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
else
toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = [NSString stringWithFormat:#"Select Start Time"];
UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
[toolBarItemlabel release];
[barItems addObject:buttonLabel];
[buttonLabel release];
[barItems addObject:flexSpace];
UIBarButtonItem *SelectBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickercancelClick:)];
[barItems addObject:SelectBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[aac addSubview:pickerDateToolbar];
[aac addSubview:theDatePicker];
CGRect myImageRect = CGRectMake(0.0f, 300.0f, 320.0f, 175.0f);;
[aac showFromRect:myImageRect inView:self.view animated:YES ];
[UIView beginAnimations:nil context:nil];
if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
[aac setBounds:CGRectMake(0,0,320, 464)];
else
[aac setBounds:CGRectMake(0,0,480, 400)];
[UIView commitAnimations];
}
// Add this if you wish to add support Orientation support for picker
- (BOOL) isViewPortrait {
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Try this, this can help you
-(IBAction)DatePickerAction:(id)sender
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
[pickerView release];
[menu release];
}
you can set the .inputView property of the text field to a datepicker..
so myTextField.inputView = datePicker;
this will replace the keyboard input view for the datePicker.
Gets a little more indepth, you will need to add a target for when the value of the date picker changes (so it updates the text field)
let me know if this is how you want to go about it.
If you still want the popup window im not sure if you can subclass UIAlertView for this..
or you can add the datePicker to a UIView instance and add that onto the screen with some fancy animation, etc....
Plenty options and im happy to help you with them...
Write in your Button Click method
self.DatePicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.DatePicker.datePickerMode = UIDatePickerModeDate;
[self.DatePicker addTarget:self
action:#selector(SetDatePickerTime:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.DatePicker];
- (void)SetDatePickerTime:(id)sender
{
[self.DatePicker removeFromSuperview];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"dd:MM:yyyy"];
NSLog(#"%#",[outputFormatter stringFromDate:self.DatePicker.date]);
}
Best method will be to create animation like it..ive tried it using extension to uiview..
try the following
in the .m file where you want to show popup just above the normal implementation
#interface UIView (AlertAnimation)
- (void)doPopInAnimation;
#end
const NSTimeInterval kAnimationDuration = 0.3f;
#implementation UIView (AlertAnimation)
- (void) doPopInAnimation {
CALayer *viewLayer = self.layer;
CAKeyframeAnimation *popInAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
popInAnimation.duration = 0.3f;
popInAnimation.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:1.2], [NSNumber numberWithFloat:0.9], [NSNumber numberWithFloat:1], nil];
popInAnimation.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:0.8], [NSNumber numberWithFloat:1.0], nil];
[viewLayer addAnimation:popInAnimation forKey:#"transform.scale"];
}
#end
further put this magical line on the event you want popup
[yourPickerView doPopInAnimation];
Refer this code
startDatePicker = [[UIDatePicker alloc] init];
startDatePicker.datePickerMode = UIDatePickerModeDate;
[startDatePicker addTarget:self action:#selector(startLabelChange:)forControlEvents:UIControlEventValueChanged];
CGSize pickerSize = [startDatePicker sizeThatFits:CGSizeZero];
startDatePicker.frame = CGRectMake(-40, -20, pickerSize.width, pickerSize.height);
startDatePicker.transform = CGAffineTransformMakeScale(0.750f, 0.750f);
UIViewController *pickerController = [[UIViewController alloc] init];
[pickerController.view addSubview:startDatePicker];
[startDatePicker release];
[pickerController setContentSizeForViewInPopover:CGSizeMake(240, 180)];
UIPopoverController *pickerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
[pickerPopover presentPopoverFromRect:startDatelabel.frame
inView:rightSideView
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
pickerPopover.delegate = self;
self.popover = pickerPopover;
[pickerController release];
[pickerPopover release];

UIPIckerView issue

I have the code below in my app. I show a UIPickerView with options, and I have a TextView placed on the same UIViewController .
when the picker is shown with options, the rest of the view seems not in focus and only when I resign the uipicker, the entire view gets focus.
what is the problem here?
-(void)viewWillAppear:(BOOL)animated {
myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[myActionSheet addSubview:pickerViewCountry];
[pickerViewCountry release];
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 27.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(hidePicker) forControlEvents:UIControlEventValueChanged];
[myActionSheet addSubview:closeButton];
[closeButton release];
[myActionSheet showInView:self.view];
[myActionSheet setBounds:CGRectMake(0, 0, 320, 250)];
UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = CGRectMake(100, 12, 210, 20);
myTextView.layer.borderWidth = 1.0f;
myTextView.layer.cornerRadius = 5;
myTextView.clipsToBounds = YES;
myTextView.layer.borderColor = [[UIColor grayColor] CGColor];
myTextView.text = #"some text";
[self.view addSubview:myTextView];
[myTextView sizeToFit];
}
You've placed your UIPickerView inside UIActionSheet. That's standart behavior for UIActionSheet. Try to use keyboardView instead like so:
UITextField* dummyTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, -20, 10, 10)];//the point is to put it outside visible view
dummyTextField.inputView = pickerViewCountry;
[dummyTextField becomeFirstResponder];//call to appear
[dummyTextField resignFirstResponder];//call do disappear
and for the buttons you can use folloving:
dummyTextField.inputAccessoryView = <some toolbar with buttons or any other view>
Happy coding :)
I will suggest you one solution with custom View.
datePickerContainer is UIView:
//datepicker
datePickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 310, 300)];
datePickerContainer.opaque = YES;
datePickerContainer.backgroundColor = [UIColor clearColor];
UIImageView *pickerNavBar = [[UIImageView alloc] init];
UIImage *barImage = [UIImage imageNamed:#"pickerbar.png"];
pickerNavBar.image = barImage;
pickerNavBar.frame = CGRectMake(0, 7, barImage.size.width, barImage.size.height);
[datePickerContainer addSubview:pickerNavBar];
[pickerNavBar release];
UIButton* blueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[blueButton setTitle:#"Done" forState: UIControlStateNormal];
blueButton.frame = CGRectMake(255, 15, 55, 30);
[blueButton addTarget:self action:#selector(hidePickerViewContainer) forControlEvents:UIControlEventTouchUpInside];
[datePickerContainer addSubview:blueButton];
[blueButton release];
//UIPickerView Initialization code
UIPickerView *pickerViewCountry = [[UIPickerView alloc] init];
pickerViewCountry.showsSelectionIndicator = YES;
pickerViewCountry.dataSource = self;
pickerViewCountry.delegate = self;
pickerViewCountry.frame = CGRectMake(0,35 , 320, 15);
[datePickerContainer addSubview:pickerViewCountry];
[pickerViewCountry release];
//set container and release
[self.view addSubview: datePickerContainer];
[datePickerContainer release];
To show datePickerContainer UIVie use this code:
-(void)showPickerViewContainer{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 150.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
To handle done button and hide pickerView use this:
-(void)hidePickerViewContainer{
//here use you custom code to handle done action
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
CGRect frameContainer = datePickerContainer.frame;
frameContainer.origin.y = 440.0f;
datePickerContainer.frame = frameContainer;
[UIView commitAnimations];
}
This is just a custom solution. Hope this help.
declare variable in header file
UIActionSheet *actionSheet;
NSString *pickerType;
Write Below code into your implementation file:
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *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];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
declare delegate methods
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}

Edit 2 uitextfield by turn with a select in uipickerview

I have 2 instances of UITextField and 1 instance of UIPickerView. UIPickerView is able to update data into
UITextField in didSelectRow:(NSInteger)row inComponent:(NSInteger)component. But how do I update 2 UITextField from UIPickerView when the user touch the UITextView?
Thanks. :)
you will need to make the UIPickerView the inputView of the textfields, i have made an example.
.h file:
#interface aViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
UITextField * textField0;
UITextField * textField1;
UIPickerView * pickerView;
UIToolbar * toolBar;
NSArray * items0;
NSArray * items1;
}
-(void)cancel;
-(void)close;
#end
.m need something like the following:
- (void)viewDidLoad
{
[super viewDidLoad];
textField0 = [[UITextField alloc] initWithFrame:CGRectMake(10.f, 20.0f, 100.f, 30.f)];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(130.f, 20.0f, 100.f, 30.f)];
textField0.backgroundColor = [UIColor grayColor];
textField1.backgroundColor = [UIColor grayColor];
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 200.f)];
// Do any additional setup after loading the view from its nib.
[self.view addSubview:textField0];
[self.view addSubview:textField1];
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320.f, 44.f)];
UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(close)];
toolBar.items = [NSArray arrayWithObjects:doneButton,cancelButton, nil];
items0 = [[NSArray alloc] initWithObjects:#"dogs",#"cats",#"llamas",#"emus", nil];
items1 = [[NSArray alloc] initWithObjects:#"bone",#"catnip",#"hay", nil];
[doneButton release];
[cancelButton release];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
pickerView.dataSource = self;
textField1.inputView = pickerView;
textField0.inputView = pickerView;
textField1.inputAccessoryView = toolBar;
textField0.inputAccessoryView = toolBar;
}
#pragma mark - actions
-(void)cancel
{
//reset the values to the original values on cancel...
//do that here.
[textField0 resignFirstResponder];
[textField1 resignFirstResponder];
}
-(void)close
{
textField0.text = [items0 objectAtIndex:[pickerView selectedRowInComponent:0]] ;
textField1.text = [items1 objectAtIndex:[pickerView selectedRowInComponent:1]] ;
[textField0 resignFirstResponder];
[textField1 resignFirstResponder];
}
#pragma mark - pickerview handling
-(float)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 130.f;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0) {
return [items0 objectAtIndex:row];
}
return [items1 objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) {
textField0.text = [items0 objectAtIndex:row];
return;
}
textField1.text = [items1 objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==0) {
return [items0 count];
}
return [items1 count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
I haven't included any clean-up code.