Identify multiple UITextField and the row index - iphone

Here is my problem:
I have an array of model class(Let's say, 'addressModel' with fields address, street and city.). Now i have a custom cell with three UITextFields for the three fields in the model class. Once the user ends editin, i want to (validate &) add the data in the particular textfield to the respective model object. (Ex: user ended editing addressTextField, then addressModel.address = addressTextField.text).
How can we identify the textfield that the user selected and the indexPath.row? In my case i need to know both? Any help?
Thank you.

If your view controller implements the UITextFieldDelegate protocol, you can receive the textFieldDidEndEditing method call. In there, you can get the tag of the field that the user was just editing. When creating your cells you can specify some kind of integer tag scheme so that you can reverse engineer what section, row, and specific field the user was actually editing at the time. You could do something like:
textField.tag = (indexPath.section * 10000) + (indexPath.row * 100) + (textFieldIndex);
Assuming you don't have more than 100 textFields per row, or 100 rows per section, this particular scheme should work.

You can validate data when user finishes editing I mean you can validate data in UITextField delegate method:
- (void)textFieldDidEndEditing:(UITextField *)textField;
here no need to find out which textfield is edited since you will get that textfield.And if you want to identified which row then you can assign tag which is nothing but row, to textfield and on that basis you will come to know which row textfield is edited.

set the separate tag of each text field .. and on didEndEditing: method use the following code....
if(textField.teg == FIRST_TAG){
// do your code
}
else if(textField.teg == SECOND_TAG){
// do your code
}...
and soo on....
may this will help you...

Related

Edit Name of cell upon tap - Swift

I want to edit the text in a cell by tapping and I was wondering if there is a way to do it other than by adding a text field. is there something in the table view delegate that I missed?
You can create cell (or any other UIResponder subclass) that conforms UITextInput protocol (or maybe UIKeyInput is enough). Here is simple example (old link)
Notice,
you should set cell.canBecomeFirstResponder = true.
To show keyboard use cell.becomeFirstResponder()
Maybe there something else, that you should do. I recommend reading docs.
...Or use UITextField :)
We use an array to display no of text label in cell.
so you can do is, you can use a text field and a button on a view.
In button action add a line of code like stringArray[2] = "new string" to replace the particular element from array and when you enter a text and press a button the element will get replaced and just reload the table view.
And you can also write the method in didSelectRowAtIndexPath, write stringArray[indexPath.row] = "new string" and reload the table view and it is done.

How to know which tableview cell's text field is changed its content

I have a tableview with around 100 text fields inside it. I added only one text field. It is reused and it is accessed using tag.
The problem is this: suppose the user has changed some textfield. Now, I want to access that text field changes during saving of the data.
How can I do this?
You are very near to your goal. You have already assigned tag to your UITextField. You can use and track tag in this delegate method.
-(void)textFieldDidEndEditing:(UITextField *)textField
{
}
First set tag of textfield with indexpath.row
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// replace text in your array
}

iPhone: UITextfield validation

I checked the existing examples here in S.O, for my case which one do you think is better,
There are many textfields in one view and I want to validate them just when the user is editing that particular textfield, cause otherwise I have to validate them together when user press next on the screen than I have show all validation messages which I dont want to bother with. I want to validate the textfield in 2 different ways, if its a number input I want to validate it in a defined range e.g if number between between 5 and 1000 or if its a text then if the lenght of the text is in a defined range e.g between 2 and 10 characters. And the user must be able to enter any input out of range.
Which is better;
Using textField:shouldChangeCharactersInRange
or something like this:
[textField addTarget:self action:#selector(validateField:) forControlEvents:UIControlEventEditingChanged];
And either the case how can I check on runtime that the number entered is in range and dont allow user to enter bigger or smaller numbers
Good one.
The answer for your question is
1) Set the tag values to all the textfields.
2) Use UIControlEventEditingDidBegin and trigger a method to validate the textfield before the currentone.
[textfield1 setTag:1];
[textfield2 setTag:2];// make sure to set tag values incrementally
[textfield2 addTarget:self #selector(validate:) forControlEvents:UIControlEventEditingDidBegin];
-(void) validate:(id) sender
{
// 10 is the number of textfields, change it accordingly.
if(sender.tag!=1 && sender.tag!=10)
{
//validate textfield with tag sender.tag-1
}
else
{
//handle first and last textfield here
}
}
Try this, probably this will solve your problem.
Actually, both of your approaches will be just fine! Using the delegate of the UITextField to call textField:shouldChangeCharactersInRange and perform the validation there is an equally good approach as using Target & Action. In both situations, you define a handler that receives the calling UITextField as an argument (one implementation of this is what #Anil Kumar suggested in his answer to tag the text fields. but this is actually not even required, you can just call isEqual: on the text fields directly to check for equality).
Personally, I tend to use delegation for these kinds of tasks though.
Update: If you want to go really fancy on this problem, you'll use Reactive Cocoa. The April Tech Talk of the Ray Wenderlich Gang had a great session on it and also discusses the issue of from validation, so it'll just match your case! :)
Take a look at TextFieldValidator developed in swift

iOS - IBAction - in which text field the user is typing

I have 2 text fields in my app and both are connected with my "devTyping" IBAction.
This method (I guess it is a method) will be called when the event "editing changed" happens.
How can I tell my method, in which text field the user is typing?
- (IBAction)devTyping {
NSLog(#"How to know in which text field the user is typing?");
}
Take a look at this question, which should be an equivalent as to what you are asking :)
You could declare the method like this:
- (IBAction)devTyping:(UITextField*)sender {
NSLog(#"The user is typing in text field %d",sender.tag);
}
And assign each textfield a tag in Interface Builder, which is just a number to identify the text field (or any other element).

display 2 picker views in one controller

hi i'm designing a view, where i have to post a query to the bank. For that i take the following input .1)Contact Method : Phone, email or Post(1st Picker) .2) Contact time :Morning, Evening,Afternoon(2nd Picker) 3) Message
now when i select the first text field ie Contact Method, a picker view should display with its datasource. and when i select the contact time text field, it should display the datasource for contact time.
Please tell me how am i to do it
you can create 3 arrays for each variant. Then, create some pointer, for example, currentArray. use this array as dataSource. so the only you have to do - change current array and call [myPicker reloadAllComponents] when your new field becomes firstResponder.