#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic)UITextField *textField;
@end
@implementation ViewController
static int loadvalue = 0;
NSMutableArray *numberarray;
- (void)viewDidLoad {
[super viewDidLoad];
numberarray = [[NSMutableArray alloc]init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"generate uitextfield" forState:UIControlStateNormal];
button.frame = CGRectMake(2.0, 2.0, 160.0, 40.0);
[self.view addSubview:button];
UIButton *elimina = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[elimina addTarget:self
action:@selector(delete last uitextfield)
forControlEvents:UIControlEventTouchUpInside];
[elimina setTitle:@"elimina" forState:UIControlStateNormal];
elimina.frame = CGRectMake(122.0, 2.0, 160.0, 40.0);
[self.view addSubview:elimina];
UITextField *textField0 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
textField0.borderStyle = UITextBorderStyleRoundedRect;
textField0.font = [UIFont systemFontOfSize:15];
textField0.placeholder = @"enter text0";
[textField0 setTag:0];
textField0.autocorrectionType = UITextAutocorrectionTypeNo;
textField0.keyboardType = UIKeyboardTypeDefault;
textField0.returnKeyType = UIReturnKeyDone;
textField0.clearButtonMode = UITextFieldViewModeWhileEditing;
textField0.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField0.delegate = self;
[self.view addSubview:textField0];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(300, 100, 300, 40)];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.font = [UIFont systemFontOfSize:15];
textField1.placeholder = @"enter text0";
[textField1 setTag:0];
textField1.autocorrectionType = UITextAutocorrectionTypeNo;
textField1.keyboardType = UIKeyboardTypeDefault;
textField1.returnKeyType = UIReturnKeyDone;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField1.delegate = self;
[self.view addSubview:textField1];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)aMethod
{
if (loadvalue > 0)
{
static int y = 100;
y = y + 50;
static int x = 200;
x = x + 50;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, y, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"primo campo";
[textField setTag:loadvalue];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"secondo campo ";
[textField setTag:loadvalue];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[numberarray addObject:[NSNumber numberWithInteger:loadvalue]];
loadvalue++;
}
else {
[numberarray addObject:[NSNumber numberWithInteger:0]];
loadvalue++;
}
}
-(void)elimina
{
NSLog(@"entro in elimina");
NSLog(@"valori di valoriloadvalue %@", numberarray);
NSInteger max = [[numberarray valueForKeyPath:@"@max.intValue"] intValue];
NSLog(@"valore di max %d", max);
UITextField *txtf = [self.view viewWithTag:max];
txtf.hidden = YES;
_textField.hidden = YES;
NSNumber *X = [NSNumber numberWithInt:max];
[numberarray removeObject:X];
}
UIButton *elimina = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[elimina addTarget:self
[COLOR="Red"]action:@selector(delete last uitextfield)[/COLOR]
forControlEvents:UIControlEventTouchUpInside];
[elimina setTitle:@"elimina" forState:UIControlStateNormal];
elimina.frame = CGRectMake(122.0, 2.0, 160.0, 40.0);
[self.view addSubview:elimina];
UIButton *elimina = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[elimina addTarget:self
action:@selector(elimina)
forControlEvents:UIControlEventTouchUpInside];
[elimina setTitle:@"elimina" forState:UIControlStateNormal];
elimina.frame = CGRectMake(122.0, 2.0, 160.0, 40.0);
[self.view addSubview:elimina];
Sorry, i've wrote it in a hurry.
The original code of "elimina" button is this:
Code:UIButton *elimina = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [elimina addTarget:self action:@selector(elimina) forControlEvents:UIControlEventTouchUpInside]; [elimina setTitle:@"elimina" forState:UIControlStateNormal]; elimina.frame = CGRectMake(122.0, 2.0, 160.0, 40.0); [self.view addSubview:elimina];
I press "generate uitextfield" and i see two uitextfield and assign to their tag 0.
I press again the same button and the method generate other uitextfield with an incremental tag (the first is 1, the second 2 , etc..) and save this value in array.
Now: if i press "elimina" button i'd like to remove the last "group" of uitextfield that i've created. In this code, i get the max value of the array and use it for hide the last group of uitextfield.
An example: if i have 3 group of uitextfield (tag 0, tag 1 and tag 2), when i press "elimina", i don't see the uitextfield with tag 2. If i press again , i don't see the uitextfield with tag 1.
With this code, i can hide only the last group. If i press again, nothing happens.
I hope I was more clear at this time.
UITextField *txtf = [self.view viewWithTag:max];
txtf.hidden = YES;
_textField.hidden = YES;
Please explain exactly what you mean by a "group" of UITextFields.
Each time the "generate uitextfield" button is pressed, two UITextFields are created and added to the view, not one. They both have the same tag. If this is what you mean by a "group", then please say so (Be specific and accurate).
You can't hide two UITextFields with the same tag using the code you posted. This is your code:
This code will retrieve the first object with the tag 'max', and hide it. It won't hide more than one object, because you only retrieve one object.Code:UITextField *txtf = [self.view viewWithTag:max]; txtf.hidden = YES;
If you give multiple objects the same tag, then it's impossible to retrieve every object with that tag using viewWithTag:. If you need to retrieve every object by its tag, you must give each object a unique tag.
If you insist on using the same tag for multiple objects, then you must write your own method to walk through the containing view's subviews (recursively), and retrieve every object whose tag matches the desired tag. You can collect and return the results in an NSMutableArray. Then you would hide every object in the array.
That code is followed by this code:
Unfortunately, you haven't shown any other code using _textField, so no one knows what that code is doing, or is expected to do. When you post incomplete code, it's impossible to give complete answers.Code:_textField.hidden = YES;
Finally, you have NSLog statements in your code. Post the log output so we know what the log is telling you about what's really happening.
aggiungieliminacampi[929:127199] valoriloadvalue values (
0,
1,
2,
3,
4,
5
)
2014-10-26 09:49:48.419 aggiungieliminacampi[929:127199] max value 5
2014-10-26 09:49:48.420 aggiungieliminacampi[929:127199] numberarray after object removal (
0,
1,
2,
3,
4
)
2014-10-26 09:49:50.032 aggiungieliminacampi[929:127199] valoriloadvalue values (
0,
1,
2,
3,
4
)
2014-10-26 09:49:50.033 aggiungieliminacampi[929:127199] max value 4
2014-10-26 09:49:50.034 aggiungieliminacampi[929:127199] numberarray after object removal (
0,
1,
2,
3
)
2014-10-26 09:49:50.982 aggiungieliminacampi[929:127199] valoriloadvalue values (
0,
1,
2,
3
)
2014-10-26 09:49:50.983 aggiungieliminacampi[929:127199] max value 3
2014-10-26 09:49:50.984 aggiungieliminacampi[929:127199] numberarray after object removal (
0,
1,
2
)
2014-10-26 09:49:51.665 aggiungieliminacampi[929:127199] valoriloadvalue values (
0,
1,
2
)
2014-10-26 09:49:51.666 aggiungieliminacampi[929:127199] max value 2
2014-10-26 09:49:51.667 aggiungieliminacampi[929:127199] numberarray after object removal (
0,
1
)
Thanks for the reply. I step back.
The idea is that when i push on "generate uitextfield" i see two utiextfield where i insert two values: the name of old bulb and the new. There should be another 3 textfields where i insert other numeric values.
For "group of uitextfield" i mean this 5 uitextfield with their values.
Each view that's given a tag number must be the only view with that tag number. No views can have the same tag number. Each view's tag must be unique. I'm not sure how to say it any other way.Uhm... interesting. By what criteria do I assign different tags and recall specifically?
Where is the code showing the other 3 UITextFields being created and added?
If a group is 5 UITextFields, and you say you want to hide or remove all 5, then the posted code is missing the creation and adding of 3 UITextFields.
.
Each view that's given a tag number must be the only view with that tag number. No views can have the same tag number. Each view's tag must be unique. I'm not sure how to say it any other way.
If you're asking for an algorithm to generate tag number, the simplest one is increase loadValue by 1 for each view created. Keep track of the first tag number in a group, and to retrieve every view in a group, start with the first number and increment until you've reached the first number of the next group, or if there isn't a next group, stop when no views have the tag.
Another algorithm would be to increment loadValue by 10 for each group, and store that value in the array. The first value (10) is assigned to the first view created. Then increment by 1, assign that to the next view (11). And so on until no more views are created. If you create 5 views each time the button is pressed, then the tags will be:
10, 11, 12, 13, 14, // first groupand the numbers stored in the array will be:
20, 21, 22, 23, 24, // second group
...etc...
10, 20Then when you retrieve the highest number from the array (say it's 20), you can increment the value (20, 21, 22, ...) until nil is returned from viewWithTag. Returning nil is how you know when to stop retrieving for a group.
There are other approaches, too, some much simpler.