I need help optimizing my code.

Discussion in 'iOS Development' started by bamhm182, Jun 3, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Alright, so I'm back to my other project for a minute. (The one with the lists) I've got everything to work. However, apparently I have no idea what I'm doing when it comes to memory.

    I'm want to make it so they can close the keyboard by clicking on the background. I think this way works fine when there is just a few Text Boxes, but I've got about 50 on one page, and about 25 on the other. I'm sure there's a better way to do this, but this how I'm doing it:
    Code:
    @synthesize TextBox1
    @synthesize TextBox2
    @synthesize TextBox3
    @synthesize TextBox4
    @synthesize TextBox5
    ...
    ...
    
    -(IBAction)backgroundPressed:(id)sender;
    {
              [TextBox1 resignFirstResponder];
              [TextBox2 resignFirstResponder];
              [TextBox3 resignFirstResponder];
              [TextBox4 resignFirstResponder];
              [TextBox5 resignFirstResponder];
              ...
              ...
    }
    I'm pretty sure that's really whats killing me.

    Also, in case anyone in interested, this is the way I decided to put in the equation, since I couldn't figure out arrays.

    Code:
    -(IBAction)buttonPressed:(id)sender;
    {
    	Value1Amount = ([TextField1.text floatValue] - ([TextField2.text floatValue] * 0.0234) - ([TextField3.text floatValue] * 0.0198) - ([TextField4.text floatValue] * 0.02));
    	Answer1Text.text = [NSString stringWithFormat:@"%f", Value1Amount];
    
    It seems to work pretty well.
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Why do you need it to be when they click the background, users are much more comfortable with pressing the done button on the keyboard I recommend you use
    Code:
    - (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
    } 
    
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    All I'm putting in is numbers, so it'd be annoying if they had the full keypad instead of just the numbers.

    EDIT: Is there a keyboard that has the done button and only the 10 numbers?
  4. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    You should definitely make an array of TextBoxes, you can use NSArray or a standard array.

    If you use an NSArray:
    Code:
    -(IBAction)backgrounPressed:(id)sender{
        for(int i=0; i<[array count]; i++){
            [[array objectAtIndex:i] resignFirstResponder];
        }
    }
    If you use an standard array:
    Code:
    -(IBAction)backgrounPressed:(id)sender{
        for(int i=0; i<array.length; i++){
            [array[i] resignFirstResponder];
        }
    }
    Instead, you can probably give FirstResponder to another object.
  5. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Only reason that I didn't already make it an array is because I have no idea how. Would you mind giving me an example of how I would do it?

    I've seen skylar's multidimensional array thread, but I think that's something other than what I'm wanting.
  6. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Standard C style array:
    Code:
    UITextField *textFields[3];
    for (int i=0;i<3;i++) {
            textFields[i] = [[UITextField alloc] init];
            [textFields[i] sizeToFit];
    
            // Do other stuff
    }
    NSArray of textFields:
    Code:
    NSArray *textFields = [NSArray arrayWithObjects:textField1, textField2, textField3, nil];
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Just keep a pointer to the current active textfield, whether it's in an array or not (arrays = win). When the background is pressed, resign only that one text field. The easiest way is to set tags for your textfields and reference it by tag.

    Tags also equal win.
  8. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Thanks GoJohnnyBoi. Arrays are a lot simpler than I thought they'd be. I'll implement that when I get a chance.

Share This Page