Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 16, 2012, 07:28 AM   #1
fstigre
macrumors regular
 
Join Date: Aug 2008
Add content form a UITextfield to an NSMutableArray everytime a button is clicked

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.
__________________
MacBook (not the pro version) 2.4 GHz - 4 GB RAM - 160 GB HD - Snow Leopard - iphone First Generation
- Mac mini - 1.6GHz First Generation with Leopard

Last edited by fstigre; Nov 17, 2012 at 09:20 AM.
fstigre is offline   0 Reply With Quote
Old Nov 16, 2012, 08:19 AM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by fstigre View Post
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
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.
__________________
dejo is online now   1 Reply With Quote
Old Nov 16, 2012, 08:56 AM   #3
fstigre
Thread Starter
macrumors regular
 
Join Date: Aug 2008
Quote:
Originally Posted by dejo View Post
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.
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.
__________________
MacBook (not the pro version) 2.4 GHz - 4 GB RAM - 160 GB HD - Snow Leopard - iphone First Generation
- Mac mini - 1.6GHz First Generation with Leopard
fstigre is offline   0 Reply With Quote
Old Nov 16, 2012, 09:03 AM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Be sure your code is not running this line in addText:
Code:
NSMutableArray *numbers =  [NSMutableArray arrayWithObjects: nil];
Do you know why?
__________________
dejo is online now   1 Reply With Quote
Old Nov 16, 2012, 12:53 PM   #5
fstigre
Thread Starter
macrumors regular
 
Join Date: Aug 2008
No, can you please explain why?

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

Quote:
Be sure your code is not running this line in addText:
__________________
MacBook (not the pro version) 2.4 GHz - 4 GB RAM - 160 GB HD - Snow Leopard - iphone First Generation
- Mac mini - 1.6GHz First Generation with Leopard
fstigre is offline   0 Reply With Quote
Old Nov 16, 2012, 12:59 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
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.
__________________
dejo is online now   0 Reply With Quote
Old Nov 16, 2012, 02:42 PM   #7
fstigre
Thread Starter
macrumors regular
 
Join Date: Aug 2008
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.
__________________
MacBook (not the pro version) 2.4 GHz - 4 GB RAM - 160 GB HD - Snow Leopard - iphone First Generation
- Mac mini - 1.6GHz First Generation with Leopard
fstigre is offline   0 Reply With Quote
Old Nov 16, 2012, 02:46 PM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Alright. So, can you now explain why you don't want this in your addText:?
Code:
numbers =  [NSMutableArray arrayWithObjects: nil];
__________________
dejo is online now   0 Reply With Quote
Old Nov 16, 2012, 03:18 PM   #9
fstigre
Thread Starter
macrumors regular
 
Join Date: Aug 2008
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.
__________________
MacBook (not the pro version) 2.4 GHz - 4 GB RAM - 160 GB HD - Snow Leopard - iphone First Generation
- Mac mini - 1.6GHz First Generation with Leopard
fstigre is offline   0 Reply With Quote
Old Nov 16, 2012, 03:32 PM   #10
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by fstigre View Post
I believe, it is because of the life cycle of the method, the array will basically die when the method finish its execution.
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
Quote:
NSMutableArray *numbers;
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?
__________________
dejo is online now   1 Reply With Quote
Old Nov 16, 2012, 03:53 PM   #11
fstigre
Thread Starter
macrumors regular
 
Join Date: Aug 2008
This is what I did which worked.

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

Quote:
@interface ViewController : UIViewController
{
NSMutableArray *numbers;
}
...
@end
... then I defined it in the viewDidLoad method.

Quote:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
numbers = [NSMutableArray arrayWithObjects: nil];
}
but left ...
Quote:
[numbers addObject:myInt];
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.
__________________
MacBook (not the pro version) 2.4 GHz - 4 GB RAM - 160 GB HD - Snow Leopard - iphone First Generation
- Mac mini - 1.6GHz First Generation with Leopard
fstigre is offline   0 Reply With Quote
Old Nov 16, 2012, 04:25 PM   #12
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by fstigre View Post
I hope this answers part of you questions.
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];
__________________
dejo is online now   1 Reply With Quote
Old Nov 16, 2012, 05:30 PM   #13
fstigre
Thread Starter
macrumors regular
 
Join Date: Aug 2008
Thanks a lot for your help!

You are awesome!
fstigre is offline   0 Reply With Quote
Old Nov 17, 2012, 09:07 AM   #14
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by fstigre View Post
Thanks a lot for your help!

You are awesome!
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.)
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
XCode: Won't let me add more than 1 button stephanie4992 iPhone/iPad Programming 2 Sep 5, 2013 03:01 PM
Add Badge for Notifications to Button RagingGoat iPhone/iPad Programming 2 Mar 20, 2013 06:57 PM
Remapping Keyboard Shortcuts: Add(+) button is unavailable pwdavenport Mac Basics and Help 0 Mar 10, 2013 09:31 AM
Navigation doesn't allow to add a bar button to it? pan17 iPhone/iPad Programming 0 Jul 9, 2012 04:25 AM
SearchBar Search Button Clicked Error loon3y iPhone/iPad Programming 6 Jun 20, 2012 06:40 PM

Forum Jump

All times are GMT -5. The time now is 10:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC