I have a tableview controller inside a navigation controller. This table simply shows one textfield in each row. In my navigation controller I have a “Done” navigation item that gets all the values entered in the textfields and shows the next screen.
The tableview controller implements UITextFieldDelegte and textFieldDidEndEditing to store the values of every textfield in its datasource (array of strings). In cellForRowAtIndexPath I also assign the delegate of the text fields to themselves. This all works well if I tap on the different textfields, I see how the datasource is properly updated.
My problem is when I do this sequence: type a value in one text field and then tap on the “Done” navigation item. I see that textFieldDidEndEditing is not called so the value is not stored. I also tried with textFieldShouldReturn and textFieldShouldReturn but nothing.
I have read many similar posts and I think the problem is that the "event" is being caught by prepareForSegue instead of textFieldDidEndEditing. If this is correct, I don’t know how to deal with this since I don’t know how to identify the indexPath of that “last edited textfield” when the Done is tapped.
Another related question: I have seen two approaches to get the indexPath of the cell containing the textfield when textFieldDidEndEditing is called:
1) textField.convertPoint + tableView.indexPathForRowAtPoint
2) tableView.indexPathForCell + textField.superview
Both seem to work well without differences (except for the issue that I just stated). Is any approach better than another?
Many thanks in advance for your help!!!
PS: if possible, in Swift code, please!
On click of done button you can add this line,
[self.view endEditing:YES];
This line will resign your currently active text field, and will call your text field delegates.
If you are displaying all text fields within same table section in that case you can assign indexPath.row to textField.tag, and using that tag you can create indexPath.
This is how you can address this:
Step 1 : Keep track of your active text field. You can keep a strong reference to it and set it whenever a textField is activated. Something like this:
self.inputField = iCell.inputField;
Step 2 : Once your Done button is tapped, add below line in action handler.
[self.inputField endEditing:YES];
PS: Alternatively, you can simply call [self.view endEditing:YES]; without the need to track the current active text field. I, personally, go with keeping reference to textfield as it helps me doing other stuff around current active textfield.
Related
I have a custom UITabelViewCell class that also implements UITextFieldDelegate
I also implement textFieldDidEndEditing so that once a field is done being edited I get a call back so that I can store the value that was typed in back in to the data array that loads the table
Seems like I have to do this or the data array never gets the type in values.
I edit various rows and when I click to the next row i get the textFieldDidEndEditing for the previous cell as expected. So far so good.
Now the problem. In the last cell I edit the cursor is still in the UITextField. I click a button to 'evaluate' the list of items in the table. Clicking the button to 'evaluate' the table causes its routine to kick off immediately. HOWEVER, the 'evaluate' finishes BEFORE get the final textFieldDidEndEditing for the field where my cursor was sitting.
That is, i do get the textFieldDidEndEditing for the field where my cursor was sitting but I don't see that call back until too late. i have already 'evaluated; the array and then afterwards I stuff the last typed value in to the data array that load the table.
How do I get the 'evaluate' to wait on the last textFieldDidEndEditing (IF there is one to come)? OR what do people typically do? Put in some kind of pause for my 'evaluate' so that there is time for the textFieldDidEndEditing callback to happen first?
I have a separate question on a second but related problem here Know the row number inside custom UITableViewCell - new wrinkle
I am trying to image gallery in apple watch. I am putting a table with two buttons in a row. So for selection of image I need to set tag to a button.
Can we do it in watch kit.
Whilst you can't set a tag on the buttons there are a couple of way you can achieve what you're after. From what I can establish from your comment you have a table with a custom row controller in it, and in each row you have 2 buttons?
As you can't tag the buttons you could assign the method they call to individual methods defined in your custom row controller. That will give you which button was pressed. To find out the index of the row controller, you could expose a variable in the header of your row controller and set that with the value that you would have used as a tag when populating the table. The button methods will be calling the method in the instance of the row controller so the variable is in scope.
The following link helped me build my table based app for WatchKit: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/Tables.html
There is no option for tag. There is one trick with the help of this trick you can identify like this
if ([[btn titleForState:UIControlStateNormal] caseInsensitiveCompare:#"YES"] == NSOrderedSame)// set your button's current state for which state you set title
{
NSLog(#"%# button pressed.",[btn titleForState:UIControlStateNormal]);
}
Set Button Title on Watch Kit
Btn.setTitle("BtnTitle")
https://developer.apple.com/library/ios/documentation/WatchKit/Reference/WKInterfaceButton_class/#//apple_ref/occ/instm/WKInterfaceButton/setTitle:
Not sure but maybe I just missed the easy way...
What I am trying to achieve is:
I have a tableView with a tableViewCell, in this tableViewCell I have a textfield in which the user can make an Input, I want to save that input when he clicks a button.
I know how to do this with the prepareForSegue function but I want do it just with the Button Action. Is that possible? Or other question, when I use the didSelectRowAtIndextPath function, can I directly access the content of the cells outlet, textfields, labels, buttons....?
Thanks for any comments...
The layout for one of my View Controllers is such: I have a scroll view embedded inside my VC. Inside my scroll view, I have a table view that consists of 5 cell. The first 3 cells consist of a textfield thats pulls its text from a dictionary and this changes depending on certain situations. These textfields are editable and so tapping on them brings up the keyboard, the issue however is that I would like my view to scroll when I tap on the text field because right now they keyboard hides the the third editable text field. Another issue is that at the moment, clicking outside teh table view doesnt cause the keyboard to be dismissed and so the only way of dismissing the keyboard is tapping on the return key. What I would like to happen is that when I tap on either one of the 3 editable fields, the scroll view ought to scroll up a certain number that I define (this is so that I can define how much to scroll depending on which row is currently selected). One of the issues I'm facing is that I can't directly reference these textfields in my VC since they're all created through one prototype cell. My thinking was that I could create a dictionary with the 3 textfields as keys and then the scrollview y coordinates as values and then use that. However , I wasn't sure how to do this in my situation (with the prototype cells). Would really appreciate if someone could show me some sample code on how to do this.
You can reference your text fields by calling cellForRowAtIndexPath: to get the UITableViewCell, then calling viewWithTag: to get your UITextField. Just assign the text fields a tag number. Also, set the text field's delegate to be your view controller so that you can respond to a user tapping to edit text.
I have a UITableViewController, a bunch of sections and rows, and for each row I added a UITextField as a subview, right aligned in the row itself.
If the users taps on the row, I locally save the indexPath, make the corresponding text field become the first responder and finally, when the keyboard appears, I make the table view scroll so that the row remains visible.
I am facing the problem to obtain the same behaviour when the user taps the text field instead. In this case the didSelectRowAtIndexPath: method isn't called, so I do not know how to tell the table view to scroll to make sure that the "selected" row is still visible.
Probably the whole process is not correct. Do you know a way to solve it?
Thanks a lot!
I'm not absolutely sure about this, but...
Set the userInteractionEnabled property of the UITextField to NO. This way, the touch goes "through" the control, tapping the UITableViewCell. When didSelectRowAtIndexPath: is called, set the userInteractionEnabled property of the UITextField to YES. When the editing is complete, change it back to NO.