UITextField in custom UITableViewCell - keyboard not dismiss - uitableview

I have custom UITableViewCell with UITextField.
The problem is that i am not able to dismiss that keyboard.
In my TVC i have UITextFieldDelegate and in viewDidLoad method i implement:
customCell.textField.delegate = self;
and of course :
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[customCell.textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[customCell.textField resignFirstResponder];
} return NO;
}
i tried implement this code into my CustomTableViewCell class.
Thanks for any help.

You already set the delegate of the textfield.
-(BOOL)textFieldShouldReturn:(UITextField *)textField; takes a textField so you didnt need to recreate the cell just to access it because it is being passed to the delegate so use it directly.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;// or YES depending on what you trying to do
}

Related

Detect edit event on a textField view in iOS

I have a cell array not in a cell collection or UItable. How do I detect an edit event on one of the cells?
First of all, set tags to your all textfields. so that you can recognise current textfield.
[textfield setTag:101];
Set delegate for each textfield.
[textfield setDelegate:self];
and at last use UITextField delegate textFieldDidBeginEditing for detecting "edit"
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField.tag == 101){
//do something
}
}
Set textfield Delegate "UITextFieldDelegate" for access the textfield methods.
And impliment the "textFieldDidBeginEditing" methods for detect an edit even in textfield.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEqual:"Your TextField Outlet name"])
//Do Something you code
//Return Value (true or false)
}
For Example:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEqual:txtContactNo])
//Do Something you code
//Return Value (true or false)
return YES;
}

Changing focus between UITextFields in cells at same UITableView

In my iPad application i have few UITableView on same view created programmatically. They must pretend one multicolumn table. Each cell contains UITextField. It's size is equal to cell's size (its the reason why i cant get UITableView's delegate methods didSelect/didDeselect row). My problem is when i begin edit one text field then try to remove focus to other textfield it needs two taps. After first tap no one of cell is not editing. Such behavior observing only inside same table. If i want to change focus to other table its possible in one tap. What i missed?
Here is my UITextField Delegate methods in Cell's class
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([_delegate checkForAccess:self]) {
if (!((CTKTextField *)textField).isEditable)
{
[_delegate callPickerUnderCell:self];
return NO;
}
else
{
[_delegate getPosition:self];
return YES;
}
}
else return NO;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (_textField.text.length == -1)
{
_textField.rightView = nil;
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *input = textField.text;
[_delegate saveEdit:self withText:input];
}
If textFieldShouldBeginEditing is called after first tap and you return YES then text field starts editing. Probably you are making endEditing to the whole tableView after that.
If you call endEditing on the specific cell in your cell's delegate instead of the whole table view it should work.
That might be caused by autocapitalizationType. Your have to dismiss the auto correction before getting your textField becomeFirstResponder. Disable it will solve the problem.
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
Edit:
I was confused, try this one:
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Try to set UITableViewCellSelectionStyleNone to your table view cells with text fields.

How to end editing mode of a TableView after first responder was called on a cell

I have made a simple program where you can add "cells" with UITextFields (ergo they are editable) with the help of a 'add Button' in the "edit mode" of a TableView.
When pressing the return key I want that the program 'jumps out' of the editing mode to the 'normal' Table View.
I guess you setup the user interface for your app in a xib or storyboard? So, make sure you connect the delegate outlet of the UITextField to your (view) controller and implement the delegate method textFieldShouldReturn: there:
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
return NO;
}
You can end the editing in the table view by using the method setEditing:animated:
[self.tableView setEditing:No animated:YES];
You would do it inside the textField delegate method.
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
if(self.tableview.editing){
[self.tableView setEditing:NO animated:YES];
}
return NO;
}
You have setEditing:animated: method and editing property on UIViewController itself. So, you could do it from those properties as well like;
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[textField resignFirstResponder];
if(self.editing){
[self setEditing:NO animated:YES];
}
return NO;
}

TextField resignFirstresponder will crash in ios 7

I am using custom cell with XIB. Now I scroll the table so the cell having the textFiled will be invisible. In this condition if i return the textField (means I call the [textField resignFirstResponder]) The app will crash and giving the error like
[UITableViewCell _didChangeToFirstResponder:]: message sent to deallocated instance
0xe05aa20.
Please let me know if you have any idea about this.
You should check couple of things:
1) In cellForRowAtIndexPath delegate, when you use dequeueReusableCellWithIdentifier you should set endEditing to YES for this view something like:
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
[cell setEndEditing:YES];
if(!cell)
{
// Rest of the logic goes here
}
2) Don't forget to set UITableViewDelegate to self which should represent a live class. So when delegate function calls, class exists.
You need to check whether your TextField is first responder. If it was then you can resign it safely
Hide Keyboard for myTextField
if ([myTextField isFirstResponder]) {
[myTextField resignFirstResponder];
}
To resign First Responder for any subview in a view
for (UIView *aSubView in self.view.subviews) {
if ([aSubView isFirstResponder]) {
[aSubView resignFirstResponder];
//break;
}
}
Use Following method to resolve issue of resignFirstResponder :
[self resignFirstResonder:self.view];
- (BOOL)resignFirstResonder:(UIView *)textView
{
keyBoardShow = NO;
if(badgeHasFullInfo == YES)
{
if (textView.isFirstResponder)
{
[textView resignFirstResponder];
return YES;
}
for (UIView *subView in textView.subviews)
{
if ([self findAndResignFirstResonder:subView])
{
return YES;
}
}
}
return NO;
}

[UITableViewCell _didChangeToFirstResponder:]: message sent to deallocated instance

I added textfield to tableview cell contentview.when i'm editing any textfield and i scrolled the tableview to bottom and dismiss the keyboard Then app crashed ,because of this reason [UITableViewCell _didChangeToFirstResponder:]: message sent to deallocated instance
Try to hide the keyboard while scrolling.
In h class
Declare a textfield
UITextField *selectedTextField;
In m class
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
selectedTextField = textField;
return YES;
}
I also had the same problem. The above solution fixed it.
All the best.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[selectedTextField resignFirstResponder];
}

Resources