Keyboard and PickerView both are visible - iphone

Im having 3 textfield in one i have implemented a UIPickerView and disable the keyboard using resignFirstResponder , but when i click on the other, both Keyboard and pickerview are visible .how do i sort this ?

create local instance of UIPickerView, and in delegate method of TextFiled textFieldDidBeginEditing check for visibility of UIPickerView, if it is Visible hide it
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(![_pickerView isHidden])
{
[_pickerView setHidden:YES];
}
//...
}

set every UITextField's Delegate to self and then just put this method in your .m class.. In this code i just set frame of yourPickerView with 500 y point instead of that you can Hide the UIPickerView..
This code Hides UIPickerView with Animation like Keyboard..
UPDATE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == TextField1) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = yourPickerView.frame;
frame.origin.y = 500;
yourPickerView.frame = frame;
[UIView commitAnimations];
return YES;
}
else if (textField == TextField2) {
[TextField1 resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = yourPickerView.frame;
frame.origin.y = 107;
yourPickerView.frame = frame;
[UIView commitAnimations];
return NO;
}
return YES;
}

You need to know what to show exactly. You can use [self.view endEditing:YES]; in order to remove the keyboard when necessary, and implement your own method to hide the picker when needed.

Set the picker as the inputView for the textfield:
myTextField.inputView = self.myPickerView;
This way you don't need to handle the displaying of the keyboard/picker at all. It's all done for you.

Simplest Solution : inputView Property
yourTextField.inputView = self.yourPickerView;
Check the Documentation.

You need to Put this line First when you are going to Display the PickerView.
[self.view endEditing:YES];
Now after that PickerView Will open & focus will not be on the TextField after writing following line so what you need to do is Put Done Button over Picker & pass the Selected value of Picker to that TextField.
Try this Code:
- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
if(txtVisitDate.isEditing == TRUE)
{
if ( [txtPlaceName isFirstResponder] )
{
[txtPlaceName resignFirstResponder];
}
[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];
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];
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.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 475)];
}
}
Hope this will help.

Related

Setting animation to always come up

I made a textfield who's first responder is a datePicker. I also attached a toolBar to to a view, so when the textfield is tapped, the toolbar slides up with an animation. Then on the toolBar their is a done button that resigns the first responder. I also want it to remove the toolBar. To do this I added this
[pickerBar removeFromSuperview];
Then to reopen it I did this
[self.view addSubview:pickerBar];
So it is activated when the textview is touched.
The problem is when I tap the textfield again the toolBar loses its navigation.
Heres most of the code
pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle=UIBarStyleBlackOpaque;
[pickerBar sizeToFit];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
CGRect pickerBarRect = CGRectMake(0, 568, 320, 44);
[pickerBar setFrame:pickerBarRect];
} else {
CGRect pickerBarRect = CGRectMake(0, 480, 320, 44);
[pickerBar setFrame:pickerBarRect];
}
//Set up the new position of the frame of the view
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(releasePicker)];
[barItems addObject:doneBtn];
[barItems addObject:doneBtn];
[pickerBar setItems:barItems animated:YES];
}
}
}
- (void)releasePicker {
[textfield resignFirstResponder];
[pickerBar removeFromSuperview];
}
- (void)pickerActivated {
[UIView animateWithDuration:.4 animations:^(void) {
[self.view addSubview:pickerBar];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){
CGRect rect = CGRectMake(0, 266, 320, 44);
[pickerBar setFrame:rect];
pickerBar.hidden = NO;
} else {
CGRect rect = CGRectMake(0, 178, 320, 44);
[pickerBar setFrame:rect];
}
}];
}
There is a difference between inputView and inputAccessoryView. This is what you want:
// setting the inputView
UIPickerView *pickerView = [[UIPickerView alloc] init];
// other configurations //
self.textField.inputView = pickerView;
// setting up the inputAccessoryView
UIToolbar *pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle = UIBarStyleBlackOpaque;
[pickerBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(releasePicker)];
[pickerBar setItems:#[flexSpace, doneBtn] animated:YES];
self.textField.inputAccessoryView = pickerBar;
This way the tool bar will be "attached" to your inputView and will automatically come up when the the text field is activated and disappear when your picker does!
Edit/Update
I did find this link that may help you, if you are interested in using a IB focused approach:
https://stackoverflow.com/a/10705161/1429262
Far simpler is to use the inputAccessoryView property on UITextField. It is irrelevant whether your inputView is a keyboard or a picker. Set your toolbar as the inputAccessoryView and it will animate in on top of your picker with the done button and then animate out on resignFirstResponder.

How do I get the correct bounds of a view after an orientation change?

I am using view.bounds to determine where to add a date picker view when a button is pressed. This works just fine if the orientation if the same as it was when the view was navigated to. Unfortunately if the orientation changes while the view is displayed the bounds are not undated, so the picker view is displayed incorrectly. I need a way to determine the actually bounds of the view. I would like something that works in both iPad and iPhone if possible because this is for a universal app. Also in the iPad version the picker view is in a splitview detail controller so I can't just swap the height and width and adjust for the navigation bar.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
//
// compute the start frame
CGRect screenRect = self.view.bounds;
CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
CGRect startRect;
CGRect pickerRect;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientation))
{
// code for Portrait orientation
startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height - pickerSize.height,
screenRect.size.width,
pickerSize.height);
}
else
{
//code for landscape
startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height - pickerSize.height,
screenRect.size.width,
pickerSize.height);
}
self.pickerView.frame = startRect;
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
self.pickerView.frame = pickerRect;
}
If I understand correctly your issue, you should try and set:
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
on your views so that they bounds are "updated" when the orientation changes.
I ended up adding the pickerview to an action sheet like so:
UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:#"Choose Date and Time"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged];
pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar 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(DatePickerDoneClick)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[aac addSubview:pickerDateToolbar];
[aac addSubview:datePicker];
[aac showFromTabBar:self.tabBarController.tabBar];
CGFloat wideth = self.view.frame.size.width;
if (UIDeviceOrientationIsLandscape([self interfaceOrientation])) {
[aac setBounds:CGRectMake(0,0,wideth, 355)];
[datePicker sizeToFit];
} else {
[aac setBounds:CGRectMake(0,0, wideth, 470)];
[datePicker sizeToFit];
}
picker = aac;
Hope that this helps others.

ToolBar at the top of UIPIckerView in xcode?

I need to add a toolbar with done button on the top of UIPickerView. I don't want to use actionSheet because I want the rest of the view to be active. I've gone for the following code:
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
[txtstate resignFirstResponder];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(10, 75, 180,20);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,75,180,10);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
textField.inputAccessoryView = toolbar;
[pickerView addSubview:toolbar];
[self.view addSubview:pickerView];
}
By using the above code my toolbar is added but it is hidden under UIPickerView and it is getting added in the middle. How can I make the toolbar come to the front (on the top of UIPickerView) ?
There is a simpler way
You can insert the picker as inputView of your text Field
and add the toolbar as inputAccessoryView to your textField
like:
textField.inputView = pickerView;
textField.inputAccessoryView = toolBar;
You can getPicker using this
-(void)getValuePicker
{
ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[ViewForValuePicker addSubview:toolBar];
valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
valuePicker.delegate=self;
valuePicker.dataSource=self;
valuePicker.showsSelectionIndicator=YES;
[ViewForValuePicker addSubview:valuePicker];
[appDelegate.window addSubview:ViewForValuePicker];
}
And its Delegete Method
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [pickerValueAry count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
id str=[ary objectAtIndex:row];
return str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selectedRowInPicker >> %d",row);
}
You can follow my answer for more Link
in viewDidLoad just add this code and when textFieldBegin just change frame like bellow...
First Create global UIView For back View of Date Picker in .h file like bellow..
UIView *viewPicker;
UIPickerView * pickerView;
and then use it like bellow..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
viewPicker.frame = CGRectMake(0, 112, 320, viewPicker.frame.size.height);
[UIView commitAnimations];
return YES;
}
add this bellow code...
viewPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 480, 320, 258)];
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0,320,44);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 320,216);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[viewPicker addSubview:pickerView];
[viewPicker addSubview:toolbar];
[self.view addSubview:viewPicker];
and in Done button clicked just set again frame like this..
-(IBAction)doneClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
viewPicker.frame = CGRectMake(0, 370, 320, 258);
[UIView commitAnimations];
}
i hope this help you...
You could try with this code:
- (BOOL)textFieldDidBeginEditing:(UITextField *)textField {
[textField resignFirstResponder];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 180, 260)];
pickerView = [[UIPickerView alloc]init] ;
pickerView.frame=CGRectMake(0, 44, 180,216);
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0,180,44);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
textField.inputAccessoryView = toolbar;
[view addSubview:toolbar];
[view addSubview:pickerView];
[self.view addSubview:view];
return YES;
}
Ok... How about just creating a vertical stack view on the storyboard editor, adding the toolbar above and the pickerview below? They don't have to be really associated to each other, they just need to be displayed and hidden at the same time. Kinda like this below:
It is really easy to do and you don't have to do coding magic.
Yeah I know this question is 7 years old but there are still people looking for solutions to this question.
you can set your UIPickerView like this way:-
-(void)showPicker{
menu = [[UIActionSheet alloc] initWithTitle:#"Select Time"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView=[[UIPickerView alloc] init];
pickerView.frame= CGRectMake(0.0, 44.0, 0.0, 0.0);
pickerView.delegate=self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
txtopsbox.inputView=pickerView;
dateTool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
dateTool.barStyle=UIBarStyleBlackOpaque;
[dateTool sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:flexSpace];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelClicked:)];
[barItems addObject:cancel];
[dateTool setItems:barItems animated:YES];
[menu addSubview:dateTool];
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 464)];
[pickerView release];
}
UPDATE
You can also do like Another Way like:-
- (IBAction)showPicker
{
if(pickerVisible == NO)
{
// create the picker and add it to the view
if(self.datePicker == nil) self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 460, 320, 216)] autorelease];
[self.datePicker setMaximumDate:[NSDate date]];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker setHidden:NO];
[self.view addSubview:datePicker];
// the UIToolbar is referenced 'using self.datePickerToolbar'
[UIView beginAnimations:#"showDatepicker" context:nil];
// animate for 0.3 secs.
[UIView setAnimationDuration:0.3];
CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
datepickerToolbarFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePickerToolbar.frame = datepickerToolbarFrame;
CGRect datepickerFrame = self.datePicker.frame;
datepickerFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePicker.frame = datepickerFrame;
[UIView commitAnimations];
pickerVisible = YES;
}
}
- (IBAction)done
{
if(pickerVisible == YES)
{
[UIView beginAnimations:#"hideDatepicker" context:nil];
[UIView setAnimationDuration:0.3];
CGRect datepickerToolbarFrame = self.datePickerToolbar.frame;
datepickerToolbarFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePickerToolbar.frame = datepickerToolbarFrame;
CGRect datepickerFrame = self.datePicker.frame;
datepickerFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height);
self.datePicker.frame = datepickerFrame;
[UIView commitAnimations];
// remove the picker after the animation is finished
[self.datePicker performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:0.3];
}
}

Disable keyboard on UITextField on edit

Since when I click the UITextField the date picker is showing along with the keyboard, I want to hide the key board operation on dob-text field?Here my code
- (void)removeViews:(id)object
{
[[self.view viewWithTag:9] removeFromSuperview];
[[self.view viewWithTag:10] removeFromSuperview];
[[self.view viewWithTag:11] removeFromSuperview];
}
- (void)dismissDatePicker:(id)sender
{
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView beginAnimations:#"MoveOut" context:nil];
[self.view viewWithTag:9].alpha = 0;
[self.view viewWithTag:10].frame = datePickerTargetFrame;
[self.view viewWithTag:11].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeViews:)];
[UIView commitAnimations];
}
- (IBAction)but
{
//[eventText resignFirstResponder];
[dob resignFirstResponder];
if ([self.view viewWithTag:9])
{
return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds ];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissDatePicker:)] ;
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.tag = 10;
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] ;
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
[UIView beginAnimations:#"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];
//[datePicker addTarget:self action:#selector(dateText:)forControlEvents:UIControlEventValueChanged];
//NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
//_dateFormatter.dateStyle = NSDateFormatterFullStyle;
//dateText.text = [NSString stringWithFormat:#"%#",
// [_dateFormatter stringFromDate:datePicker.date]];
//[self.tableview reloadData];
}
I added the resignfirstresponder also,since its showing the same error
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];//dob textfield
}
Use UITextField's delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == dob-text )
//load picker here
return NO; //hide keyboard
else
return YES; //show keyboard
}
A better solution is to set the UIPickerView as the inputView for the UITextField.
And you can set the UIToolBar as the inputAccessoryView of the inputView.
This way iOS will handle the displaying of all UIPickView.
Just set the properties top the correct views:
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.inputToolbar;
On another note, getting views with viewWithTag: methods means that the you are looping thru all the views. Why not create properties for these views. It will be less messy and might be faster.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
datePicker.hidden = NO;
[textField resignFirstResponder];
}
try in textFieldDidBeginEditing
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
}
First of all a textfield should not have a popover - neither picker nor popover nor anything
Best solution would be to put a button - type custom - set image like a date selector image and add selector - in that case you would n't have any issues w.r.t textfields popover and the look would be good as well and you can customize it as per your requirement.

Subview above toolbar

I use [self setToolbarItems:items]; to add buttons to toolbar I want to display subview to be above all even teh toolbar I use the following code , but it be behind the tool bar
any suggestion how to make it above
pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,744, 768, 216)];
mytab = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 700, 768, 44)];
pickerView.alpha=0.0;
mytab.alpha=0.0;
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[self.view addSubview:pickerView];
[self.view bringSubviewToFront:pickerView];
mytab.tintColor=[UIColor blackColor];
UIBarButtonItem * bt1=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(_cancel)];
UIBarButtonItem * flx=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *arr=[[NSArray alloc] initWithObjects:flx,bt1,nil];
[mytab setItems:arr];
[self.view addSubview:mytab];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
pickerView.alpha=1.0;
mytab.alpha=1.0;
[UIView commitAnimations];
[pickerView release];
[mytab release];
[bt1 release];
[flx release];
[arr release];
you are adding the tool bar after the picker ... add the tool bar fist and then the picker...
actually i think you should be presenting the picker as a modal view controller's view..
EDIT:
You can see how a modal view controller is displayed in appple's sample codes. Look for presentModalViewController method
SimpleEKDemo
Metronome
I simply subtract 44 from the Y of pickerview and toolbar