Resolved Add content form a UITextfield to an NSMutableArray everytime a button is clicked

Discussion in 'iOS Programming' started by fstigre, Nov 16, 2012.

  1. fstigre, Nov 16, 2012
    Last edited: Nov 17, 2012

    fstigre macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #1
    Hi,

    I will start by saying what I need to acomplish. I have a button and a UITextfield, what I want to happen is...

    1- Every time the button is clicked, whatever it is in the textfield to be put in an NSMutableArray (only numbers).
    2- Display each item in the array to the screen (in a UILabel), each in a separate line.
    3- Display the sum of all items in the array in a different UILabel.

    My problem is getting the values from the textfield and appending them to the array.

    The following code does exactly what I need BUT the values are NOT coming from a textfield, they are individual NSNumbers.

    Code:
    - (IBAction)addText:(id)sender { 
        
        NSNumber *myInt;
        myInt = [NSNumber numberWithInt:5];
        NSNumber *myInt2;
        myInt2 = [NSNumber numberWithInt:10];
        NSNumber *myInt3;
        myInt3 = [NSNumber numberWithInt:20];        
            
        NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: myInt, myInt2, myInt3,nil];   
        
        int result =0;    
        NSString *string = @"";    
        
        for(int i = 0; i < [numbers count]; i++)    {
            
            int nums = [[numbers objectAtIndex:i] intValue];        
            self.display.numberOfLines=0;
            string = [string stringByAppendingFormat:@"Item: %i \n", nums];
            
            result += [[numbers objectAtIndex:i] intValue];
        }
            
        self.display.text = string;   
        self.total.text = [NSString stringWithFormat:@"Total: %i", result];    
        
    }
    I tried the following code but it only outputs whatever is in the textfield at the time of the execution, it doesn't retain previous values. I tried declaring the array and the textfield outside the method (addText) thinking that it was because of the life sycle of the method but it didn't work.

    Code:
    - (IBAction)addText:(id)sender { 
      
        NSNumber *myInt =[NSDecimalNumber decimalNumberWithString:self.inputText.text];       
        NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: nil];
        
        [numbers addObject:myInt];  
        
        int result =0;       
        NSString *string = @"";    
        
        for(int i = 0; i < [numbers count]; i++)    {
            
            int nums = [[numbers objectAtIndex:i] intValue];        
            self.display.numberOfLines=0;
            string = [string stringByAppendingFormat:@"Item: %i \n", nums];
            
            result += [[numbers objectAtIndex:i] intValue];
        }
            
        self.display.text = string;   
        self.total.text = [NSString stringWithFormat:@"Total: %i", result];    
        
    }
    Any idea what am I doing wrong?


    Thanks a lot.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Didn't work how? Did you get run-time or compile-time errors? More details please. Show your code for this as well.

    Basically, your issue boils down to scope. You want your array to live longer than a single execution of a method. I think an instance variable in your viewController would work if you don't want your array to have scope outside of that viewController. If you do need it to have wider scope, you'll need consider other data-persistence approaches.
     
  3. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #3
    No, I didn't get any compile errors, it just didn't make a difference, it worked but it didn't retain previous values it only showed the last value.

    I will play around with the scope of my array as you suggested, I just needed to know if I was on the right track which it looks like I'm, I just need to play around more.

    Thanks a lot for your help.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Be sure your code is not running this line in addText:
    Code:
    NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: nil];
    Do you know why?
     
  5. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #5
    No, can you please explain why?

    and to be quite honest I didn't understand your statement either.

     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Hmm, if you don't know what that line of code does, why do you have it in your code?

    How comfortable are you with the fundamentals of Objective-C programming? If not much, I suggest you step away from the real coding and take the time to learn them before you return to tackle this issue.
     
  7. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #7
    I didn't know what were you referring to with "addText", but I just realized that it is how I called my method. Sorry for the miss understanding, this is code I'm using for practicing purposes and I didn't put too much attention to how I named the method, my bad.

    I now know exactly what you meant with that statement, sorry.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Alright. So, can you now explain why you don't want this in your addText:?
    Code:
    numbers =  [NSMutableArray arrayWithObjects: nil];
     
  9. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #9
    I believe, it is because of the life cycle of the method, the array will basically die when the method finish its execution.

    I hope I got that right.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    If you make numbers a local variable within your addText: method, correct.

    But, I'm asking about something else, to make sure you understand it correctly.
    So, let's say you now have
    as an instance variable of your class.
    Do you still want:
    Code:
    numbers =  [NSMutableArray arrayWithObjects:nil];
    in your addText: method? If not, why not? If so, why so?
     
  11. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #11
    This is what I did which worked.

    Declared my NSMutableArray in my header file like so...

    ... then I defined it in the viewDidLoad method.

    but left ...
    in the addText method because this will add the numbers every time the method is called.

    I hope this answers part of you questions.

    Thanks a lot for trying hard to make me understand these basic and important programming concepts.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    It does. Thanks.

    Just a clarification, though. When you said you "defined it in the viewDidLoad", that's not exactly the case, at least to me. You defined/declared numbers in your header file. What you are doing in viewDidLoad is assign it a value, which happens to be an empty array. What you coded is equivalent to a common alloc-init, so I would word what you did there as "initializing" the value (which you need to do in order to add objects to it). And you could also have just initialized it with:
    Code:
    numbers = [[NSMutableArray alloc] init];
    or even:

    Code:
    numbers = [NSMutableArray array];
     
  13. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
  14. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #14
    Mark the topic as resolved. (Go to your first post, click edit, go advanced, and click on the spinner to the left of the topic title and pick resolved.)
     

Share This Page