need help bulding a peace of code need to add a object to an array
I have this table view which has to add button. this add button opens a view where the user can enter something in a textfield. when the user is done he pressed a done button which put the text in the textfield in to 2 public variables then it spawn the tableview again but when the table view is spawned again it has the entered text as a object i need this process so it can be done unlimited times. but i don't know who i can make this please help
i have been traying to make this code by my self and this is what i got to
this is the tableview
-(void)viewWillAppear:(BOOL)animated{
if(appdel.sExerciseName != NULL){
NSString *newitem = appdel.sExerciseName;
appdel.newExerciseArray = [[NSMutableArray alloc]initWithObjects:newitem, nil];
NSLog(@" number of objects in array is %i",[appdel.newExerciseArray count]);
int x = 0;
do{
NSLog(@"23 pos is : %i and object is %@",x,[appdel.newExerciseArray objectAtIndex:x]);
for (int y =0; y<[appdel.newExerciseArray count]; y++) {
[exercises addObject:[appdel.newExerciseArray objectAtIndex:x]];
}
x++;
}while (x<[appdel.newExerciseArray count]);
appdel.sExerciseName = NULL;
[Table reloadData];
} }
this is the donebutton
-(IBAction)DonPressed{
appdel.sExerciseName = NameField.text;
appdel.sExerciseTimes = timesField.text;
NSLog(@"name %@ and times is %@ button pressed1",appdel.sExerciseName , appdel.sExerciseTimes);
exercisesViewControler *detailViewController = [[exercisesViewControler alloc] initWithNibName:@"exercisesViewcontroler" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
so this code works first time but when i due it second time it just overwrite the old object开发者_C百科 / item in the array with contains the data off the table view
sry for my english and please keep in mind when you answer keep it simple so i can understand as a noob developer
the problem is, that you are creating new instances of exercisesViewController when you are done, by calling:
[[exercisesViewControler alloc] initWithNibName:@"exercisesViewcontroler" bundle:nil];
But what yout want to do is to keep the object. Instead you could do the following:
[self.navigationController popViewControllerAnimated:YES];
So your new method will look like:
-(IBAction)DonPressed{
appdel.sExerciseName = NameField.text;
appdel.sExerciseTimes = timesField.text;
NSLog(@"name %@ and times is %@ button pressed1",appdel.sExerciseName , appdel.sExerciseTimes);
[self.navigationController popViewControllerAnimated:YES];
}
精彩评论