My problem:
I use IOS 5 and prototype cells.
In 'cellForRowAtIndexPath' function I set up my cell like that:
cell = [aTableView dequeueReusableCellWithIdentifier:#"datiInvioCell"];
UITextField *testo = (UITextField*)[cell viewWithTag:1];
testo.placeholder = iv.descrizione;
testo.text = iv.valore;
Now, I must store the result of the cell editing into an array. I used to do it like that:
-(void) textFieldDidEndEditing:(UITextField *)textField {
InputInvio *iv = [self.cellDettagli objectAtIndex:textField.tag];
iv.valore = textField.text;
}
but I can't use textField.tag anymore! Any idea? Thanks.
here you use static tag for your every textField so its get only one value which last.....
so here when you use bellow line just replace may be after that its work..
UITextField *testo = (UITextField*)[cell viewWithTag:indexPath.row];
here every textfield has its own tag...
hope , this help you..
:)
Related
How can I read UITextField value in an IBAction? I'm creating UITextField programmatically. So I can't set #property and #synthesize using Xcode. The code to generate UITextField is as follows:
for(i=0; i<[fieldName count]; i++)
{
UITextField *name = [fieldName objectAtIndex:i];
frame = CGRectMake(fromLeft, fromTop, totalWidth, totalHeight);
name = [[[UITextField alloc] initWithFrame:frame] autorelease];
name.borderStyle = UITextBorderStyleRoundedRect;
name.returnKeyType = UIReturnKeyDone;
//name.placeholder = [fieldName objectAtIndex:i];
name.autocapitalizationType = UITextAutocapitalizationTypeWords;
name.adjustsFontSizeToFitWidth = TRUE;
name.keyboardType = UIKeyboardTypeDefault;
[name addTarget:self action:#selector(doneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
[scroller addSubview:name];
fromTop = fromTop + 40;
}
Now I want to read values of each textbox in a button click (IBAction). Can anyone help me please?
You could use something like this to loop through all UITextFields that are subviews of self.view and add their text to a NSMutableArray:
for (UITextField *field in self.view.subviews) {
if ([field isKindOfClass:[UITextField class]]) {
if ([[field text] length] > 0) {
[someMutableArray addObject:field.text];
}
}
}
if your doneEditing: looks like this doneEditing:(id)sender then you can say:
UITextField *field = (UITextField *)sender;
NSString *myText = field.text;
EDIT:
To access a UITextField without setting as an instance variable you need to tag it when you create it:
[textField setTag:1];
then whenever you want to access it you can get it from its parent view by the tag:
UITextField *myTextField = [scroller viewWithTag:1];
NSString *myString = myTextField.text;
in your case, set the tag to i+1 for example to have all the textfield with unique tags.
implement the IBAction function like the following:
-(IBAction) doneEditing:(UITextField*)sender
{
NSString * val = sender.text;
}
try using the UITextFieldDelegate , i think its better for your case.
add each UITextField a Tag and by that you will recognise the UITextField.
NSString *value = sender.text;
If inside an IBAction, of course.
Which ignores that there is a set of text fields, and a single button action.
The simplest solution to the overall problem would be to store a set (or array) of text fields in an instance variable, and iterate over that set in the button action. But that is a rather coarse approach; it is probably better to use the text field delegate method and store text values directly in an array, using the button to trigger the save.
In addition, Apple HIG would tell you that you should update your data model as the text fields are edited, rather than use a "Save" button - which is poor UX design - unless, of course, the values of individual fields can interact.
I have a number like 12345678910111213 and I need to pass it from one method(cellForRow) to another(button action method). The simplest way which I used to use is to pass it through a button tag. In this case it is impossible(?). I can also create a property for it but what about encapsulation? I want to know really RIGHT(and preferably simple) way for doing things like that. Thanks in advance!
Well you can actually attach the value to the UIButton. When you have the value you want to pass and you have a reference to the button:
static char kMyObject;
objc_setAssociatedObject(myButton, &kMyObject, [NSNumber numberWithInt:myInt], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
On the other side, when you receive the action with the button as id:
- (void)myAction:(id)sender
{
UIButton *myButton = (UIButton*)sender;
NSNumber *number=objec_getAssociatedOject(myButton,&kMyObject);
}
You cannot pass it as a tag as Saad said. You can use NSDecimal numbers here.
#Saad cannot use double, as it will lose precision.
In this integer tag you can store a pointer (case of 32 bit address) to a class/structure/whatever what represents bigint.
For example:
UIButton *button = [UIButton ...];
button.tag = (int)[[MyBigInt alloc] initWithString:#"12131312312312312"];
after:
MyBigInt *bigInt = (MyBigInt *)button.tag;
...
[bigInt release];
I'm going to make some assumptions here, because I just when through something similar.
The UIButton with the action is in a UITableViewCell.
You have an underlying source for all your data (ie. An array with all your data in it).
You have easy access to your tableView.
First, you need to get the cell which contains the button:
UITableViewCell *cell = nil;
for (UIView *view = sender; view; view = view.superview) {
if ([view isKindOfClass:[UITableViewCell class]]) {
cell = (UITableViewCell *)view;
break;
}
}
Next, you need to get the indexRow for that cell:
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Finally, you have to get access to your data:
ModelClass modelObject* obj = [self.data objectAtIndex:indexPath.row];
Now, you can make any changes you need to your model.
i have
if(indexPath.row==0)
{
cell.lbl.text=#"abc";
cell.txtField.placeholder=#"abc"
cell.txtField.tag=104;
delegate.copyNameString = [NSString stringWithFormat:#"%#", [cell.txtField text]];
} else if(indexPath.row==1)
{
cell.lbl.text=#"home";
cell.txtField.placeholder=#"xyz";
cell.txtField.tag=105;
}
I am trying to retrieve the text which is in textfield, at row 0 and row 1.
i am trying to retrieve text on the textfield on the basis of tag.
How can i retrieve text?
Thanks
You can retrive your textfield like this and when you got your required textfield ,then get text.
UITableViewCell *myCell = (UITableViewCell *)[*yourTableViewName* cellForRowAtIndexPath:*passindexPath*]];
UITextField *mytextFiled = (UITextField *)[myCell viewWithTag:104];
the direct answer to your question is to use viewWithTag. In your example, [cell viewWithTag:105] will give you the text field. That said, your example has other issues.
First, if you can access the cell and the cell has a txtField property than you don't need the tag. Just use cell.txtField when you have the cell. Tags are typically used when you don't have a property holding a reference to the view you need. If you can't access the cell, the tag won't help you.
Second, you shouldn't add subviews directly to the cell. Instead you should add them to the cells contentView. The docs explain how to create a custom UITableViewCell fairly well.
NSString *textValue=((UITextField *)[self.view viewWithTag:104]).text;
You do like this,
UITextField *urtextFiled = (UITextField *)[self.view viewWithTag:104];
NSString *textValue=[urtextFiled text];
In which method you are trying to retrieve the text? I think you have to set delegate for your textfield and retrieve the text from this delegate method -(BOOL) textFieldShouldReturn:(UITextField *)textField
I have a Tableview I want to change the value of textField on the first row of second section?
I assign it tag value which lets say is 2.
Now how to access that TextField with tag.
Give a tag to that text field and by using that tag you can change the value.
UITextField *tempField = (UITextField *) [self.view viewWithTag: tag];
tempField.text = #"Your data";
If you wnat to access your textField inside some table view methods so you can do this:-
UITextField *tView=(UITextField *)[cell.contentView viewWithTag:2];
If you have to access your textField outside some tableVIew method than you have to do :-
lastSelectedPath=indexPath;do where you have added your text field.You will need its indexPath.Make it a global variable.
UITableViewCell *prevoiusCell=(UITableViewCell *)[*yourtabelviewname* cellForRowAtIndexPath:lastSelectedPath];
UITextField *tView=(UITextField *)[prevoiusCell.contentView viewWithTag:2];
For some reason, I'm having trouble with making a textfield the first responder.
I have a UITableView with two rows. Each row has a label and a UITextField. The textfields are tagged kLoginRowIndex = 0 and kPasswordRowIndex = 1. As you might have guessed, I use this for setting login and password.
If the user taps on the return button when editing the login textfield, I want the password textfield to get the focus. Unfortunately, the password textfield doesn't accept the focus. Here is my code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"%s:(textField.tag:%d)", __FUNCTION__, textField.tag);
[textField resignFirstResponder];
if(textField.tag == kLoginRowIndex) {
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:kPasswordRowIndex inSection:0]];
UITextField *nextTextField = (UITextField *)[cell viewWithTag:kPasswordRowIndex];
NSLog(#"(nextTextField.tag:%d)", nextTextField.tag);
NSLog(#"canBecomeFirstResponder returned %d", [nextTextField canBecomeFirstResponder]);
NSLog(#"becomeFirstResponder returned %d", [nextTextField becomeFirstResponder]);
} else {
[self validate:textField];
}
return NO;
}
This is the log output:
-[SettingsViewController textFieldShouldReturn:]:(textField.tag:0)
(nextTextField.tag:1)
canBecomeFirstResponder returned 1
becomeFirstResponder returned 0
What I tried:
returning YES instead of NO
removing the call to canBecomeFirstResponder (which is just for debugging purposes)
Any hints are appreciated!
After playing with the suggestion of tmadsen, I found the error. The mistake is this line:
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:k
It returns a new cell, not the one currently on the screen. I replaced it with
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:kPasswordRowInde
and now it works as expected.
On a side note, I found out that 0 is the default value for the tag property, so it's probably not so clever to use it.
0 is the default value for the tag property so you'll probably want to use something other than 0, otherwise you will most likely return the superview when you call viewWithTag:
It's been a while since I developed for the iPhone, and I have never used the tag that you show in your code. But you can do what you want by making the textfields properties of your class. If you do that, and let's say you name those properties loginTextField and passwordTextField, then you can let the next textField be focused as follows:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([self usernameTextField] == textField) {
return [[self passwordTextField] becomeFirstResponder];
}
else {
// your validating code...
}
return NO;
}
But as I said, it's been a while and I don't know this tag-thing you talk about, so maybe it's some new best practice, but the above code should be working