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.
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.
Any idea what am I doing wrong?
Thanks a lot.
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.
Last edited: