Only allowing 2 of 4 textfields to have data

Discussion in 'iOS Programming' started by thedon1, Feb 7, 2013.

  1. thedon1 macrumors 6502

    Joined:
    Jun 26, 2010
    #1
    I have four text fields. I only want the user to be able to enter a value in two of them (any 2). Once the second box has had data entered into it, I want the other fields to be no longer be editable. If the user deletes data from one of the boxes, I want the others to be editable again.

    I was thinking that maybe creating an array of the text fields and then in my delegate function of textFieldDidEndEditing:, I could do a check of how many of those text fields are not empty. If it's 2, then disable editing in the other text boxes. I don't know how to do that, though.

    Any help would be great.

    Thanks
     
  2. ArtOfWarfare, Feb 7, 2013
    Last edited: Feb 7, 2013

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    You have IBOutlets to each of the text fields, I'd assume, so why not just have something like:

    Code:
    int nonEmpty = 0;
    nonEmpty += textField1.text.length ? 1 : 0;
    nonEmpty += textField2.text.length ? 1 : 0;
    nonEmpty += textField3.text.length ? 1 : 0;
    nonEmpty += textField4.text.length ? 1 : 0;
    textField1.enabled = textField2.enabled
        = textField3.enabled = textField4.enabled
        = nonEmpty < 2 ? TRUE : FALSE;
     
  3. thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #3
    Hi thanks for the reply. I'm going to try it tonight.

    Would you mind explaining the code a little bit? From my understanding, your looking at the length of the strings of each text box and then assigning a 1 or 0 depending on if it's empty or not. This is counting incrementally.

    I've never seen syntax like that with the ? 1:0.
    I also don't know what's going on here textField1.enabled = textField2.enabled

    Some info would really help my understanding.

    Thanks
     
  4. ArtOfWarfare, Feb 7, 2013
    Last edited: Feb 7, 2013

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Okay. It sounds like you have a limited knowledge of C. Obj-C is just an extension to C that adds a lot of support for objects and classes, so all valid C is also valid Obj-C.

    First off, I should mention I had a typo. I originally had a variable called count but I decided to rename it nonEmpty and I forgot to replace count every time with it. Second, I added an initial assignment to the nonEmpty variable. Both of the changes are in my above post.

    Next, I used three operators:
    • += Increases the variable on the left by the amount on the right.
    • (condition) ? (expression 1) : (expression 2) If condition is true (non-zero), returns expression 1. If condition is false (zero), returns expression 2.
    • = Assigns the variable on the left with the amount on the right. EVALUATES FROM RIGHT TO LEFT.

    These are all standard C operators. The lines that look like
    Code:
    nonEmpty += textField1.text.length ? 1 : 0;
    are first looking at the length of the text in textField1. If the length of the text is zero, that is, if the field is empty, then the condition of the ?: operator is false, and it returns the value on the right of the colon, 0. This 0 is added to nonEmpty with the += operator, which, as you might know, will leave the value of nonEmpty unchanged (the field was empty, so that's the desired end result.)

    If the length of the text is anything else, that is, if the field isn't empty, then the condition of the ?: operator is true, and it returns the value on the left of the colon, 1. This 1 is added to nonEmpty with the += operator, meaning nonEmpty will be incremented by 1 (the field wasn't empty, so that's the desired end result.)

    I next utilized the fact that C (and Obj-C) treats all whitespace (that is, spaces, tabs, and returns) the same way. So I broke up the last expression into 3 lines, to hopefully make it easier to read (thus why I indented the second two lines of the expression and started them with the operator.)

    textField's enabled property allows you to easily enable and disable text entry into a field by assigning it true (if it should be editable) or false (if it's shouldn't be editable.)

    The = operator works from right to left, meaning you should look at what comes after the final = in a chain of ='s to see what everything is being assigned to. In
    Code:
    nonEmpty = 0;
    i is being assigned zero. In
    Code:
    a = b = c = 0;
    a, b, and c are all being assigned to 0.

    So after the final = in my code I have
    Code:
    nonEmpty < 2 ? TRUE : FALSE; 
    If nonEmpty is less than 2, it'll return what's on the left of the colon, TRUE, and assign that to each of the four textField's enabled properties (it'll enable all textFields if only one or zero contain text.) If nonEmpty is 2 or greater, it'll return what's on the right of the colon, FALSE, and assign that to each of the four textField's enabled properties (it'll disable all textFields if two or more contain text.)

    Technically, the final ?: isn't really necessary and you could just have
    Code:
    = nonEmpty < 2;
    as the final line, because that will return 1 (TRUE) if it's true or 0 (FALSE) if it's false, but it helps improve the readability of the code a bit, I think.
     
  5. thedon1 thread starter macrumors 6502

    Joined:
    Jun 26, 2010
    #5
    I started doing some more research before your reply and spotted the typo you mentioned above. I looked for some info on ternary operators and figured it out.

    I edited the code a little bit as I needed the 2 textfields that had text in them to remain editable.

    Here's what I got.

    Code:
    -(IBAction)Test
    {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        
        int t = 0;
        
        
        a = txt1.text.length>0 ? 1 : 0;
        b = txt2.text.length>0 ? 1 : 0;
        c = txt3.text.length>0 ? 1 : 0;
        d = txt4.text.length>0 ? 1 : 0;
        
        t = a+b+c+d;
        
        if (t==2) {
            
            
            
            txt1.enabled = a>0 ? YES : NO;
            txt2.enabled = b>0 ? YES : NO;
            txt3.enabled = c>0 ? YES : NO;
            txt4.enabled = d>0 ? YES : NO;
        }
        
        if (t<2) {
            
            
            
            txt1.enabled = YES;
            txt2.enabled = YES;
            txt3.enabled = YES;
            txt4.enabled = YES;
        }
    
    }
    
    It works perfectly.

    Thanks a lot for your first post because it gave me a way to approach this problem. That direction is so important.

    Thanks even more for your post above with the explanations. This is going to help me so much, I really do appreciate it. You could have just told me to go re-read a book or something but you didn't. Thanks a lot mate.

    Even though my question was probably really basic to a lot of people on this board and some will interpret that as me not wanting to do any work on my own, but that's def not the case. Sometimes just getting a little thread of help can set someone off in the right direction. I'm lucky i got much more than a thread here.
     

Share This Page