How to dismiss a UIPicker that is connected to a UITextField - objective-c

I have a UITextField, and when pushed a UIPickerView comes up to choose a value. How do I get the UIPickerView to dismiss once a value is chosen. Someone in another thread told me to resignFirstResponder the textfield, but my code isn't working. Any ideas? NOTE: I have two text fields and UI Pickers, that's why I have the 'if' 'else' statement.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
[agePickerView removeFromSuperview];
return YES;
}
else
{
[relationshipTextField resignFirstResponder];
[relationshipPickerView removeFromSuperview];
return YES;
}
}

Try the following code which may solve your problem.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
}
else
{
[relationshipTextField resignFirstResponder];
}
[pickerView removeFromSuperview];
return YES;
}
or resign your keyboard on following method of UIPickerView.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

Please try resigning the UITextField in UIPickerViewDelegate method as follows.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.ageTextField resignFirstResponder];
pickerView.hidden = YES; //Show the pickerview again when you neeed it
}

Implement the UIPickerViewDelegate protocol and implement the following method:
- (void) pickerView: (UIPickerView*) pickerView
didSelectRow: (NSInteger) row
inComponent: (NSInteger) component
{
[self.ageTextField resignFirstResponder];
}

Related

resignFirstResponder not getting called inside textFieldShouldClear

I am trying to implement search bar in one of my page.
I am not using regular search bar due to its design.
What I have is as below.
UIImageView above UIView (textfield background)
UITextField above UIImageView (textfield)
I am using delegates for UITextField.
In code I have searchTF.clearButtonMode = UITextFieldViewModeWhileEditing; to show the clear button.
Search is working fine but the problem is in delegate of clear button.
I have below code
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
NSLog(#"clicked clear button");
[textField resignFirstResponder]; // this is not working
// also below is not working
// [searchTF resignFirstResponder];
}
return YES;
}
When I click clear button, I get NSLog of text "clicked clear button", however the keyboard doesn't get dismissed.
Any idea why keyboard is not getting dismissed when I have
Edit 1
Even I tried as below using [self.view endEditing:YES];, but still its not working.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[self.view endEditing:YES];
[self hideAllKeyboards];
}
return YES;
}
Additional to my comment I made some testing and here are my results:
I've just implemented a UITextField with all delegate methods like this:
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(#"Should Clear");
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"Begin editing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"End editing");
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"Should begin editing");
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(#"Should end editing");
return YES;
}
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSLog(#"Change char");
return YES;
}
As soon as you hit the clear button the log outputs:
2014-07-26 11:08:44.558 Test[36330:60b] Should Clear
2014-07-26 11:08:44.558 Test[36330:60b] Should end editing
2014-07-26 11:08:44.559 Test[36330:60b] End editing
2014-07-26 11:08:44.560 Test[36330:60b] Should begin editing
2014-07-26 11:08:44.561 Test[36330:60b] Begin editing
As you can see the shouldBeginEditingand the didBeginEditing methods get called after clearing so the resignFirstResponder in textFieldshouldClear gets called just before a new becomeFirstResponder is called by shouldBeginEditing or didBeginEditing.
I believe your textfield IBOutlet is properly connected ,Although you can try
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
I am not sure what was the problem, but I solved calling the dismiss keyboard method after some interval using NSTimer.
Below is what I did.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(hideAllKeyboards) userInfo:nil repeats:NO];
}
return YES;
}
-(void) hideAllKeyboards {
[searchTF resignFirstResponder];
}
This way, after clicking the clear button, keyboard is getting dismissed.
It would be great if someone post answer as why this is happening. I will mark that answer as accepted.
Edit 1
As per Daniel answer, I did below.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
isFilterOn.text = #"no";
[textField resignFirstResponder];
textField.text = #"";
[self myTextFieldDidChange];
}
return NO;
}
In myTextFieldDidChange, I am showing the filtered list and un-filtered list based on the text I have in UITextField.

Why is controlTextDidEndEditing being called when I send setEditable?

//1:
-(void) TextFieldEdit:(CDTextField *)textField{
[textField setEditable:YES];
}
//2:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification{
NSTextField* textField = (NSTextField *)[aNotification object];
[textField setEditable:NO];
}
I hope setEditable at time 1, and close it at time 2.
But I found when I sent setEditable Xcode immediately called the controlTextDidEndEditing:.
Why?
EDIT: The first method is invoked via the following sub-classed method:
-(void)mouseDown:(NSEvent *)event {
if ([event type]==1)
{
NSInteger key=[event modifierFlags];
if ( key & NSCommandKeyMask)
{[self.delegate CDTextFieldEdit:self]; }
else
if (event.clickCount >1)
{[self.delegate CDTextFieldClicked:self]; return;}
}
[super mouseDown:event];
}

UIPickerView is not removed when row selected

I have a UIPickerView that appears when a textfield is selected:
-(void) showPicker
{
[self.genere resignFirstResponder];
pickerGenero = [[UIPickerView alloc] initWithFrame:CGRectMake(0,215,320,0)];
pickerGenero.delegate = self;
pickerGenero.dataSource = self;
pickerGenero.showsSelectionIndicator = YES;
[self.parentViewController.tabBarController.view addSubview:pickerGenero];
pickerGenero=nil;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [genreArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.genere.text =[genreArray objectAtIndex:row];
[self.pickerGenero removeFromSuperview];
}
But is not removed when a row is selected. Textfield is updated with the selected value, but the picker view is not removed. I've tried to use as a trial .sethideen=True but it doesn't work, too.
Many thanks
Actually your's is not the correct way of coding. When you click the textField you just hide the keyboard by using resignFirstResponder and invoking pickerView.Instead of doing like that, you have to add your pickerView as inputView for genere textField. and try this code then
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.genere.text =[genreArray objectAtIndex:row];
[genere resignFirstResponder];
}

UITextField in a UIActionSheet only calling some delegate methods

The below code shows that when a user does a long press gesture on a Table View Cell, then a UIActionSheet launches with a UITextField inside of it. When tapping the UITextField, the keyboard launches, and textFieldShouldBeginEditing and textFieldDidBeginEditing get called, but the text field won't accept the key taps.
Hitting the return key won't trigger the delegate methods, but tapping one of the UIActionSheet buttons will trigger textFieldShouldEndEditing and then textFieldDidEndEditing.
I'm setting the textField to become the first responder, so I'm not sure why it's not accepting input from the keyboard. Any suggestions?
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
// only when gesture was recognized, not when ended
if (gesture.state == UIGestureRecognizerStateBegan)
{
// get affected cell
SinTableViewCell *cell = (SinTableViewCell *)[gesture view];
// get indexPath of cell
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// do something with this action
NSLog(#"Long-pressed cell at row %d", indexPath);
AppDelegate_Shared *appDelegate = (AppDelegate_Shared*)[UIApplication sharedApplication].delegate;
//setup UITextField for the UIActionSheet
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 170, 320, 200)];
textField.borderStyle = UITextBorderStyleBezel;
textField.backgroundColor = UIColorFromRGB(0XFFFFFF);
textField.text = #"";
textField.delegate = self;
[textField setKeyboardType:UIKeyboardTypeAlphabet];
[textField setKeyboardAppearance:UIKeyboardAppearanceAlert];
//setup UIActionSheet
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:#"Add Notes"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Save", nil];
[asheet showFromTabBar:appDelegate.tabBarController.tabBar];
[asheet setFrame:CGRectMake(0, 100, 320,380)];
[asheet insertSubview:textField atIndex:0];
//[textField becomeFirstResponder];
//memory management
[textField release];
[asheet release];
}
}
#pragma mark -
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"textFieldDidBeginEditing");
[textField becomeFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(#"textFieldShouldEndEditing");
return YES;
}
//should save the notes value here, I think
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"textFieldDidEndEditing");
[textField resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(#"textFieldShouldClearEditing");
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"in textFieldShouldReturn");
return YES;
}
Your question is a little bit old but not marked as answered, so if it is helpful for you or other viewers I post my solution from my own SO question. I started up with the same problem as you had and ended up in the fact that UIActionSheet really eats up some important events which are necessary to get the keyboard working properly.
My linked posted code unfortunately works only in portrait orientation, anyway it does it.
Is UITextFieldDelegate and UIActionSheetDelegate in your header?
If not, there could be problems while setting the textfield.delegate to self

UIPickerview in iphone

How to place UIPickerView programmatically in a subView with out using Interface Builder?
Hey! U can make UIPickerView programmatically....
In- viewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,320, 500)];
[self.view addSubview:pickerView];
[pickerView setDelegate:self];
[pickerView release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return ; //give components here
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return ; //give rows here
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return ; // give titles }
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//Do the step here
}
And in your viewController.h class never forgot to do this
#interface viewController : UIViewController <UIPickerViewDelegate>
Hope this may help u.....for more help follow the link ..