delete or hide more uitextfield

Discussion in 'iOS Programming' started by sgs1, Oct 24, 2014.

  1. sgs1 macrumors newbie

    Joined:
    Jul 14, 2014
    #1
    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!
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    Definitely. Use the
    Code:
     tags and we can help you out if you elaborate more
     
  3. sgs1 thread starter macrumors newbie

    Joined:
    Jul 14, 2014
    #3
    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];
    
    }
    
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. sgs1 thread starter macrumors newbie

    Joined:
    Jul 14, 2014
    #5
    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.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. sgs1, Oct 26, 2014
    Last edited: Oct 26, 2014

    sgs1 thread starter macrumors newbie

    Joined:
    Jul 14, 2014
    #7
    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.


    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.

     
  8. chown33, Oct 26, 2014
    Last edited: Oct 26, 2014

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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 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.
     
  9. sgs1 thread starter macrumors newbie

    Joined:
    Jul 14, 2014
    #9
    That's right. I've seen problem with 3 uitextfields and i didn't create the other 2 for this problem.

    Thanks for the idea. I'll try it and i'll let you know about it.
     

Share This Page