I Created five UITextField using for loop i want to clear the textfield values when i Click the Clear button but only clear the last textfield values.i want to clear all text fiel values how can i solve this any one Help me Thanks
In your clear method write like:
for (UIView *addedView in [self.view subViews])
{
if([addedView isKindOfClass:[UITextField class]])
{
UITextField *myText = (UITextField *)addedView ;
myText.text = #"";
}
}
Here each sub view is taken from the self.view and check if it is a UITextField or not. If it is a UITextField then we set #"" to it.
for (UIView *view in self.view.subviews)
if([view isKindOfClass:[UITextField class]])
[(UITextField*) view setText:#""];
You can do this by using following:
-(void)loopMethod
{
lastTextField.tag=100;
}
-(void)buttonClick
{
UITextField *textField=(UITextField*)[self.view viewWithTag:100];
textField.text=NULL;
}
Related
I have lot of text fields statically placed in static TableViewcells. How can access the text in it without creating referencing outlet for each textfield? I have tried taking subviews of cell. It's not working. Any alternatives?
I tried this. It's not working.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView
cellForRowAtIndexPath:indexPath];
for (UIView *subview in [cell subviews]){
if([subview isKindOfClass:[UITextField class]]){
UITextField *inputField = (UITextField *)subview;
NSLog(#"%#",inputField.text);
}
}
return cell;
}
You could use an IBOutletCollection. That lets you connect them all to one array, where you can access them by index, or use setValue:forKey: to address them all at once.
You can always iterate through every subviews of your cell to find a UITextField. Something like:
NSArray *subviews = [cell subviews];
for (view in subviews) {
if([view isKindOfClass:[UITextField class]]) {
//Do what you want with your UITextField
}
}
Follow these steps:
Set tag to your textField:
textField.tag = kTextFieldTag;
Retrieve subView of cell with that tag
UITextField *refTextField = [cell viewWithTag:kTextFieldTag];
Here, kTextFieldTag is constant.
I've created a table where each cell holds both a text label and a text field. I'm adding the textfields as such [cell addSubview:passwordField]; and from a visual perspective they appear and are editable, etc....
The problem arises when I attempt to retrieve the entered values from the textfields. I iterate over the cells and try to grab the subview (IE the textfield), however, my iteration only discovers the text label.
Here is the code I'm using to search:
for(NSInteger i =0; i < [tableView numberOfRowsInSection:0]; i++){
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
UIView* subView = [[cell.contentView subviews]lastObject]; // I've also tried object at index here
// Anything beyond this is just matching....
Another approach I took was recursively searching the subviews, but, again that yielded no results.
You have added your textField on subView of cell.
[cell addSubview:passwordField];
While you're trying to find it on cell.contentView.
Add your textField as a subView of cell.Contentview
[cell.contentView addSubview:passwordField];
And find it in this way -
for(UIView *view in [cell.contentView subviews])
{
if([view isKindOfClass:[UITextfield class]])
{
UITextField *textField = (UITextField *)view;
NSLog(#"%#",textField.text);
}
}
Why not have a datasource mapped to the TableView and just retrieve / update the values in the datasource. You can then call reloadRowsAtIndexPaths to load just the row you just changed. Trying to iterate through the TableView rather than just updating the datasource seems very inefficient.
Instead of UIView* subView = [[cell.contentView subviews]lastObject]; you can try to find it as:
for(UIView *view in [cell subviews])
{
if([view isKindOfClass:[UITextfield class]]){
// view is the reference to your textfield
}
}
That way you can add other UIViews as subviews and still get the reference of the textfield without having to keep track of its subview index.
2 things occur to me:
In the long run it'll be easier to create a UITableViewCell which contains a UITextField which is accessible as a property. You can either use a nib to layout the cell or do it programmatically in the cells init method. This approach will make your code easier to manage.
You need to consider cell reuse. If you are reusing cells (which you should be) then you will need store the fetch the value from the textfield before it is reused.
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];
}
}
In my custom cell there is a UILabel and one UIButton. Whenever I click on that UIButton the UILabel's height is increased as per content of the label.
The problem is that how can I get that UILabel that is a subview of a cell. Every cell contains a label and a button but only that UILabel is increased that the cell button is clicked, others remain as they are.
Thanks in advance.
You can go through all subviews in the cell and see which one is a UILabel:
for(UIView *v in [cell subviews])
{
if([v isKindOfClass:[UILabel class]])
//you can put more checks to see if height is to be increased/decreased
[v setFrame:CGRectMake(origin.x,origin.y,width,height)];
}
For this you have need to assign TAG for every component..
Try bind the method to your button may work for you.
-(void)buttonClick:(id)sender {
UITableViewCell *cell=(UITableViewCell*)[sender superview];
NSArray *subviews=[cell subviews];
for (int i=0; i<[subviews count]; i++) {
id object=[subviews objectAtIndex:i];
if([object isKindOfClass:[UILabel class]]){
//do stuff here
}
}
}
The easiest way is to assign the tag property to the labels. That way in your button action method you can find the relevant label. For example:
- (void) myButtonAction: (UIButton *) sender
{
UILabel *theLabel = (UILabel *) [sender.superview viewWithTag: MYLABELTAG];
// Do something with theLabel
}
What this code does is finds the label in the same view as the button is in.
I wrote a short example on how to get labels from tablecells here: https://stackoverflow.com/a/9864169/407488
But i think what you want is to increase all labels in all cells at once? Then just use reloadData and change a member variable before calling it (when touching the button). You read that member in your cellForRow: code and assign the height appropriate.
- (IBAction)onClick1:(id)sender {
// Make sure it's a UIButton
if (![sender isKindOfClass:[UIButton class]])
return;
NSString *title = [(UIButton *)sender currentTitle];
}
I understand how to get the title and other current values but I don't see how I can get the value of the tag property.
I've got a test project here where I just used:
NSInteger i = [sender tag];
You can simply call:
NSInteger the_tag = ((UIView*)sender).tag;
Each UIButton is a subclass of UIView which contains the tag property.