I have a text field In that user can enter values from 1-10
at the bottom of the text field i have button.
i need to add the values in text filed to array when user clicks done button
for this i wrote code like this.
- (void)viewDidLoad
{
[super viewDidLoad];
PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h
}
-(IBAction)doneClick:(id)sender
{
[PainlevelsArray addObject:txtField.text ];
// [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
NSLog(@"PainlevelsArray *** %@",PainlevelsArray);
[self.navigationController popToRootViewControllerAnimated:YES];
}
But every time array displays only the recently added one value only in log when button clicks, like this
Output on Console
========================
2012-05-07 10:11:15.689 ESPT[796:10d03] PainlevelsArray *** (
2
)
2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
4
)
My array gets empty every time view loads, and adde a fresh entry when button clicks
But i need like this
2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
4,
5,
2,
6,
8,
)
All the velues enters are adde/inserted to the existing array.
Thanks in advance
in ExerciseStartViewController
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ExerciseResult *result = [[ExerciseResult alloc] init];
[self.navigationController pushViewController:result animated:YES];
[result release];
}
in current/ ExerciseResult viewcontroller
- (void)viewDidLoad
{
[super viewDidLoad];
PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h
}
-(IBAction)doneClick:(id)sender
{
[PainlevelsArray addObject:txtField.text ];
// [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
NSLog(@"PainlevelsArray *** %@",PainlevelsArray);
[self.navigationController popToRootViewControllerAnimated:YES];
}
You need to use a global array.
Best possible solution is declare an array in AppDelegate.h file and alloc-init this array in applicationdidfinishlaunch method.
Use UserDefaults: These are used to save your application data in key-value format. The values/objects stored as userdefaults always remain in application, until you have not removed it from sandbox. You can use following code to store an array in userdefaults.
In AppDelegae applicationDidFinishLuanch method you can retrieve this array from userdefaults using: