make textfield become first responder - iphone

In my table I use UITextField istead of UILabel (with enabled = 0).
Now pressign an add button, the last cell shall become editable. I tried the following:
UITextField *textField = [subviewsOfCell ObjectAtIndex:index];
[textField becomeFirstResponder];
but I doesn't get it editable. (I know that I get the correct cell, because textField.text = #"test"; displays correctly). What must I do?

Just try doing this:
UITextField *textField = [subviewsOfCell ObjectAtIndex:index];
textField.enabled = YES;
[textField becomeFirstResponder];
Let me know if that works for you.

Related

Get subview (UITextField or UILabel) of UITableViewCell -- two ways, one works, one doesn't - why?

I have a UITableViewCell designed via a Storyboard that contains a UITextField and UILabel. I want to update the values of these views when cellForRowAtIndexPath is called.
I first tried to get references to the textfield and label like this:
UITextField *textfield = (UITextField*)[cell.contentView viewWithTag:10];
UILabel *label = (UILabel*)[cell.contentView viewWithTag:20];
This worked for the UILabel, but I was only able to get the reference to UITextField the first time the cell was created, I was able to set the original value (textField.text) but never update it. If I inspect the array of subviews I can see that the UITextField is there, I just can't get to it with viewWithTag. I switched to this (below) and it works:
UITextField* textField = [[cell.contentView subviews] objectAtIndex:0];
UILabel *label = [[cell.contentView subviews] objectAtIndex:1];
Why doesn't the first approach work? What's the right way to do this?
This code is in the context of:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DetailCell" forIndexPath:indexPath];
UITextField* textField = [[cell.contentView subviews] objectAtIndex:0];
textField.text = #"<something>";
UILabel *label = [[cell.contentView subviews] objectAtIndex:1];
label.text = #"<something else>";
return cell;
}
Sorry. This was just a bug at my end, I apologize for wasting your time. I was overriding the value for textField.tag that had been set in the Storyboard UI. (I relied on it being the value set in Storyboard which was not the case). The original code runs correctly. Still not sure which approach is better. Is it safer to assume a specific index or a specific tag value? Neither of these seems safe to me.

Switching between UITextField cells in UITableView

Very beginner obj-c question.
I have grouped TableView with two sections http://uaimage.com/image/c6c9ca23 . In first section I have custom cells with UITextField's in them. I need to implement Input Accessory View For keyboard with additional buttons "Next", "Prev" (to switch between text fields in first section) and "Done"(to dismiss the keyboard) http://uaimage.com/image/62f08045 .
Question is: what do I need to implement possibility to switch between text fields in cells in first section of TableView by tapping input Accessory buttons?
Do I need to tag cells or text fields and if so, how can I retrieve their value when user will tap on Input Accessory buttons? Or it is a better approach to do this?
Thanks, Alex
I assume say u have 3 UITextField ie txt ,txt1, txt 2 with tags 0 1 and 2; Now add UITableViewCell *cell in .h file.
EDIT :
Now to get references of all textField from current tableView cell add this delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
cell = nil;
cell = (UITableViewCell *)[textField superView];
return YES;
}
Now in Input Accessory prev button action do this:
-(IBAction)previousBtn:(id)sender
{
UITextField *txt = (UITextField*)[cell viewWithTag:0];
UITextField *txt1 = (UITextField*)[cell viewWithTag:1];
UITextField *txt2 = (UITextField*)[cell viewWithTag:2];
if(txt.isFirstResponder)
{
[txt resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if(txt1.isFirstResponder)
{
[txt1 resignFirstResponder];
[txt becomeFirstResponder];
}
else if(txt2.isFirstResponder)
{
[txt2 resignFirstResponder];
[txt1 becomeFirstResponder];
}
}
Now in Input Accessory next button action do this:
-(IBAction)nextBtn:(id)sender
{
UITextField *txt = (UITextField*)[cell viewWithTag:0];
UITextField *txt1 = (UITextField*)[cell viewWithTag:1];
UITextField *txt2 = (UITextField*)[cell viewWithTag:2];
if(txt.isFirstResponder)
{
[txt resignFirstResponder];
[txt1 becomeFirstResponder];
}
else if(txt1.isFirstResponder)
{
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if(txt2.isFirstResponder)
{
[txt2 resignFirstResponder];
[txt becomeFirstResponder];
}
}

how to implement textfield in table the way it showing in image

i want to add textfield on tableview each and access the textfield's text by using their tag.
how can i do that see below image.
// To get the text from UITextField , add the bellow code to after you initialize the Textfield
[yourTextField addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
and this method
- (void) getTextFieldValue:(UITextField *)textField{
NSLog(#"GetTextvalue");
if (textField.tag == 0) {
NSLog(#"1--->%#",textField.text);
}else if(textField.tag == 1){
NSLog(#"2--->%#",textField.text);
}else if(textField.tag == 2){
NSLog(#"3--->%#",textField.text);
}
}
You have to customize your tableViewCell you can do that in your cellForRowAtIndexPath method.
While you create your cell in the method you can do following:
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 101;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
[textField release];
After that where every you want to access your text field you can do following:
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
Hope this is what you are looking for..
Update
As you can see I assigned tag 101 as static. This can be any number but make sure that you don't have any other view with the same tag in the same cell. If you have in another cell than its not an issue.
Now at anyplace if lets say in didSelectRowAtIndexPath you can do following:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *myTextField = (UITextFiled*)[cell viewWithTag:101];
This way you will get your cell first. Now your cell has only one view with 101 tag so you can get your text field regardless you have same tag for other textField because the cell is not same for all.
Cheers
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
myTextField.tag=indexPath.row;
This way assign 10 textfields tags.
After in textfield delegate method you can retrieve that text from the different 10 tags.

How to make accessible multiple custom textfield in iPhone sdk

Below is my code, As given in code If my table has more than one row than I can't access that text field means I can type text but my keyboard doesn't disappear and if I have only one row than it is accessible, Can anyone help me how to make more than one textfield accessible because I want to create it as dynamically not by fix code
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 270, 21, 21)];
textField1.placeholder = #"0";
textField1.delegate = self;
textField1.text = [numberofevent objectAtIndex:indexPath.row];
cell.accessoryView = textField1 ;
[self textFieldShouldReturn:textField1];
[textField1 addTarget:self action:#selector(textChanged:) forControlEvents:UIControlEventEditingDidEnd];
[textField1 release];
And this one is for resign keyboard code
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField1 resignFirstResponder];
return YES;
}
and last one is for my text value changed event
- (void) textChanged:(UITextField *)source {
}
So, this code run for any number of row, but it can't give access when row is more than one so how can I generate multiple textfield within one code as per app needed and how can i access it.
I think there is an issue involving textField1. First of all, it is apparently an instance variable because you are accessing it from at least two methods, but you release it at the end of what I presume is cellForRowAtIndexPath (your first block of code here) and after that point really shouldn't be referring to textField1 any more (even though it is retained by the cell). So I don't think textField1 should be an ivar at all. The second problem is that you are always referring to textField1 in textFieldShouldReturn: rather than textField. Who knows what textField1 will refer to by the time textFieldShouldReturn: is called, but textField will always refer to whatever text field should be returning.
In short, I think you need to change this:
[textField1 resignFirstResponder];
to this:
[textField resignFirstResponder];
I'm also not sure why you are calling textFieldShouldReturn yourself from within cellForRowAtIndexPath. There is also another UITextFieldDelegate method that you should probably be using rather than adding your own action/target to the text fields.
This might fix your main problem, unless there are other issues in the rest of your code.
EDIT: Let me try to explain this even more clearly. You have one text field per row. By the time all of your cells are created in cellForRowAtIndexPath, textField1 refers only to the very last text field that was created. Your textFieldShouldReturn method always refers to textField1. It should refer to its argument, textField, which corresponds to the actual text field that was first responder when the return was initiated.
Note that this is not a good code, but should get you up and running. Reuse the view and stuff on your code, set up the textfield properly etc. Basically what's wrong with yours at the moment is you're using 1 textfield for every cell.
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell)
{
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 270, 21, 21)];
textField.placeholder = #"0";
textField.delegate = self;
textField.text = [numberofevent objectAtIndex:indexPath.row];
cell.accessoryView = textField;
}
// do something else here if the cell is reused
}

how to add a textfield on last row in tableview in iPhone?

when i press the new button i need to add a textfield on last row of tableview.
can any one explain the flow.
Thanks in advance.
Just i tried this scenario.
Create a one Boolean variable like this
BOOL newButtonPress = NO;
then fallow the below code
-(IBAction)newButtonClicked:(id)sender{
if (self.newButtonPress == NO) {
//if new button press then newbuttpress is yes and reload the tableview
self.newButtonPress = YES;
[self .reloadTableview reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(self.newButtonPress == YES)//creating noOfRows when newbutton pressed
{
return [self.itemNames count]+1;
}
else {
return [self.itemNames count];
}
}
Then add the below code into the cellForRowAtIndexPath method
//when new button is pressed adding new cell
if (self.newButtonPress == YES) {
if([self.itemNames count] == [indexPath row]){
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//creating the textfield if new button is pressed
UITextField *extraField = [[UITextField alloc] initWithFrame:CGRectMake(80, 6, 100, 30)];
extraField.borderStyle = UITextBorderStyleNone;
extraField.textAlignment = UITextAlignmentCenter;
[extraField becomeFirstResponder];
[extraField setDelegate:self];
[cell addSubview:extraField];
[extraField release];
}
}
If you just want the text field to appear at the end of the table, why not consider using UITableview's tableFooterView property?
In that case you don't have to ensure that you return the UITextField in your delegate methods.
You should nearly be able to drag drop a new UITextField in your Tableview if you are using InterfaceBuilder or XCode 4.
I am very new to iphone coding,i want to share my thoughts. If that is wrong please ignore.
In cellForRowAtIndexPath method u need to set a textfield in
if(indexPath.row==numberOfRows-1) condition.
In button pressed method u can set textField.hidden=NO and in viewdidload textField.hidden=YES.