|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
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];
}
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];
}
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. |
|
|
|
0
|
|
|
#2 | |
|
Quote:
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.
__________________
|
||
|
|
1
|
|
|
#3 | |
|
Quote:
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 |
||
|
|
0
|
|
|
#4 |
|
Be sure your code is not running this line in addText:
Code:
NSMutableArray *numbers = [NSMutableArray arrayWithObjects: nil];
__________________
|
|
|
|
1
|
|
|
#5 | |
|
No, can you please explain why?
and to be quite honest I didn't understand your statement either. Quote:
__________________
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 |
||
|
|
0
|
|
|
#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.
__________________
|
|
|
|
0
|
|
|
#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.
__________________
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 |
|
|
|
0
|
|
|
#8 |
|
Alright. So, can you now explain why you don't want this in your addText:?
Code:
numbers = [NSMutableArray arrayWithObjects: nil];
__________________
|
|
|
|
0
|
|
|
#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.
__________________
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 |
|
|
|
0
|
|
|
#10 | ||
|
Quote:
But, I'm asking about something else, to make sure you understand it correctly. So, let's say you now have Quote:
Do you still want: Code:
numbers = [NSMutableArray arrayWithObjects:nil];
__________________
|
|||
|
|
1
|
|
|
#11 | |||
|
This is what I did which worked.
Declared my NSMutableArray in my header file like so... Quote:
Quote:
Quote:
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 |
||||
|
|
0
|
|
|
#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]; Code:
numbers = [NSMutableArray array];
__________________
|
|
|
|
1
|
|
|
#13 |
|
Thanks a lot for your help!
You are awesome! |
|
|
|
0
|
|
|
#14 |
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 03:03 AM.







I support the
Linear Mode
