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];
}
Related
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;
}
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.
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;
}
I have an application where the user selects items in a tableView and he clicks in a textField inside the row and places a number (for quantity). What I want to do is that when the user clicks the textField, the symbols and numbers keyboard would appear, or even better, a number keypad. This is what I have so far:
In my SearchResultTableViewCell.m class, the class I use for my custom tableViewCell, I have this function:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField reloadInputViews];
return YES;
}
So far, it does not work. I also tried to do
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[quantity setKeyboardType:UIKeyboardTypeNumberPad];
[quantity reloadInputViews];
return YES;
}
Where quantity is the IBOutlet UITextField as declared in the .h file.
You should declare IBOutlet of textfield in custom tableview cell's .h file & set keyboard type in
1) awakeFromNibin custom cell's .m file,
-(void)awakeFromNib
{
[self.textField setKeyboardType:UIKeyboardTypeNumberPad];
}
2) cellForRowAtIndexPath of tableview's datasource method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Init you cell as you are doing right now
CustomCell* cell;
[cell.textField setKeyboardType:UIKeyboardTypeNumberPad];
}
You're doing all this work at "textFieldShouldReturn", which is the end of entering in text into some text field (the quantity text field or some other text field).
Why not do this work in "textFieldShouldBeginEditing:(UITextField *)textField", or why not just simply set the keyboard type of the Quantity text field in the storyboard or xib file?
You are using the wrong method to set the keyboard!
textFieldShouldReturn is called when the return button on the keyboard is pressed.
You must set the keyboard type when you are initializing the textField in the code or set the property in .xib or if you want to set the keyboard type when user clicks the textField then put your code in the following method textFieldShouldBeginEditing:(UITextField *)textField
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;
}