Disable keyboard on UITextField on edit - iphone

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.

Related

Keyboard and PickerView both are visible

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.

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];
}
}

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;
}
}

iPhone UIDatePicker very slow

Im using a UIDatePicker on ModeTime,
but is very slow??
here the code:
#import "U2YAccountController.h"
#implementation U2YAccountController
#synthesize timePicker = _timePicker;
#synthesize timeLabel= _timeLabel;
#synthesize scheduleLabel = _scheduleLabel;
- (id)init
{
self = [super init];
if (self) {
// Set title
self.title = #"Reminder Settings";
CGRect dosageImageRect = CGRectMake(10, 30, 300, 62);
UIImageView *dosageImage = [[UIImageView alloc] initWithFrame:dosageImageRect];
[dosageImage setImage:[UIImage imageNamed:#"dosageReminderButton.png"]]; //image was a button, now just image change name of png!
dosageImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:dosageImage];
[dosageImage release];
UIButton *timeReminderButton = [UIButton buttonWithType:UIButtonTypeCustom];
[timeReminderButton setImage:[UIImage imageNamed:#"reminderTimeButton.png"] forState:UIControlStateNormal];
[timeReminderButton setFrame:CGRectMake(10, 110, 300, 44)];
[timeReminderButton addTarget:self action:#selector(timeReminderButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[timeReminderButton setAdjustsImageWhenHighlighted:NO];
[self.view addSubview:timeReminderButton];
self.timePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 415, 220, 180)]autorelease];
self.timePicker.datePickerMode = UIDatePickerModeTime;
[self.timePicker addTarget:self action:#selector(timePickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.timePicker];
//[self.timePicker setHidden:YES];
self.scheduleLabel = [[[UILabel alloc]initWithFrame:CGRectMake(22, 64, 200, 20)]autorelease];
self.scheduleLabel.textColor = [UIColor colorWithRed:12/255.0 green:113/255.0 blue:186/255.0 alpha:1];
self.scheduleLabel.text = #"Schedule 6 of 7 days"; //updated from web site!!
[self.view addSubview:self.scheduleLabel];
self.timeLabel = [[[UILabel alloc]initWithFrame:CGRectMake(213, 122, 80, 20)]autorelease];
self.timeLabel.textColor = [UIColor colorWithRed:12/255.0 green:113/255.0 blue:186/255.0 alpha:1];
self.timeLabel.backgroundColor = [UIColor clearColor];
self.timeLabel.text = #"";
[self.view addSubview:self.timeLabel];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma"];
}
return self;
}
- (void) timeReminderButtonPressed :(id) sender {
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)] ;
[UIView beginAnimations:#"PickerUp" context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationBeginsFromCurrentState:YES];
self.timePicker.frame = CGRectMake(0, 237, 320, 180);
[self.view addGestureRecognizer:recognizer];
[recognizer release];
}
- (void) timePickerChanged:(id) sender {
today = [sender date];
NSString *dateString = [dateFormat stringFromDate:today];
self.timeLabel.text = dateString;
}
- (void) viewTapped :(id) sender {
[UIView beginAnimations:#"PickerDown" context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationBeginsFromCurrentState:YES];
self.timePicker.frame = CGRectMake(0, 415, 320, 180);
[self.view removeGestureRecognizer:recognizer];
}
- (void) dealloc {
[dateFormat release];
[_scheduleLabel release];
[_timeLabel release];
[_timePicker release];
[super dealloc];
}
#end
So why is my picker slow and getting stucked?
what im i missing??
ps. I have commented the nsDateFormatter to find if that was the problem, but no change...
thanks a lot!
My initial reaction:
Allocating an NSDateFormatter isn't a cheap prospect. In your case, you're using the same format string over and over again, so you should REALLY just be allocating a single NSDateFormatter, saving it as an instance variable, and then using that when the UIDatePicker action fires. That should speed things up considerably.
Another thing to add to previous answer is that you've forgot to commit your animation. I'll even suggest you to use animateWith... methods of the UIView class rather than beginAnimation/commitAnimation as it should be faster (at least by apple).
And one more to go. The gesture recognizer... why not create it with the view at the init time? why create it every time you call the picker?