Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sgs1

macrumors newbie
Original poster
Jul 14, 2014
28
0
Hi everybody!

I have a big problem with hide more uitextfield that i've generate by button press. Can i add here the code that i've wrote?

Thanks!
 

sgs1

macrumors newbie
Original poster
Jul 14, 2014
28
0
Hi! Thanks for the reply.
This is the code. Another problem is that if i delete a "group of uitextfield" when i press on "generate button" i see the new group in the same position of the old.

Code:
#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];

}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
In this code:
Code:
    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];
Please explain what you expect the code in red to do. There is no method named "delete last uitextfield", because that's an invalid method name.

If you're expecting it to invoke the elimina method that does exist, then you need to change what's between the parentheses of the @selector().

If you're expecting it to do something else, then please explain exactly what you're expecting it to do.


Rules of Thumb:
1. Post your code.
2. Describe what you expected to happen.
3. Describe what actually happened.
4. Be specific and accurate.

You did #1.

You haven't done #2 or #3. In your first post you said you had a "big problem", but you haven't described what that problem is.

You haven't done #4, either, but without #2 and #3, #4 has very little to refer to.

An example of #2 is, "I expected a press in the button titled "elimina" to call the elimina method of my ViewController object".

An example of #3 is, "Two buttons appear, titled "generate uitextfield" and "elimina". The "generate uitextfield" button works as expected, calling my aMethod method. Nothing at all happens when I press the button titled "elimina".".

An example of not following #4 is where you say there's a "generate button", but no such button appears in your code. Maybe you've left out some code (incomplete code posted), or maybe you're referring to another button (not specific), or maybe you're referring to the "generate uitextfield" button (not accurate). That's too many maybe's to answer.
 

sgs1

macrumors newbie
Original poster
Jul 14, 2014
28
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
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.

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:
Code:
    UITextField *txtf = [self.view viewWithTag:max];
    txtf.hidden = YES;
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.

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:
Code:
    _textField.hidden = YES;
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.


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.
 

sgs1

macrumors newbie
Original poster
Jul 14, 2014
28
0
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).

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.


You can't hide two UITextFields with the same tag using the code you posted. This is your code:
Code:
    UITextField *txtf = [self.view viewWithTag:max];
    txtf.hidden = YES;
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.

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:
Code:
    _textField.hidden = YES;
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.


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.

Uhm... interesting. By what criteria do I assign different tags and recall specifically?

Here is the output of the log. I've pushed 5 times the "generate" and "delete" button.

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
)
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
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.

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.


Uhm... interesting. By what criteria do I assign different tags and recall specifically?
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 group
20, 21, 22, 23, 24, // second group
...etc...
and the numbers stored in the array will be:
10, 20
Then 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.
 
Last edited:

sgs1

macrumors newbie
Original poster
Jul 14, 2014
28
0
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.
.

That's right. I've seen problem with 3 uitextfields and i didn't create the other 2 for this problem.

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 group
20, 21, 22, 23, 24, // second group
...etc...
and the numbers stored in the array will be:
10, 20
Then 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.

Thanks for the idea. I'll try it and i'll let you know about it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.