Reputation: 695
i have a tableview with a list of categories. if the user taps on add button an alertview with a textfield and 'ok' button appears.and the text entered in the textfield should be added to the table view.I am able to add it but its not getting added to the plist.so if we move to another view and come back that just vanishes.how to make it stay like that.
-(void)viewDidLoad
{
NSString *fileForCategoryList = [[NSBundle mainBundle] pathForResource:kCATEGORY_LIST ofType:kP_List];
self.arrayForCategories = [[NSMutableArray alloc]initWithContentsOfFile:fileForCategoryList];
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.arrayForCategories removeObjectAtIndex:indexPath.row];
[self.tableViewC reloadData];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:kYOUR_TITLE message:kEMPTY delegate:self cancelButtonTitle:kCANCEL otherButtonTitles:kOK, nil];
self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[self.myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:self.myTextField];
[myAlertView show];
[myAlertView release];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([self.myTextField text])
{
[self.arrayForCategories addObject:[self.myTextField text]];
}
[self.tableViewC reloadData];
}
Upvotes: 1
Views: 1304
Reputation: 26400
You have to write that data into the plist file and then save it. Hope you are accessing the file from the bundle of the application. Make a copy of it initially and save it the Documents directory. Then when you add the new data, save that data into the plist by writing into the plist file of the documents directory.
[yourData writeToFile:filePath atomically:YES];
Update
See if this tutorial helps.
EDIT
First read the data from the plist into an array (mutable so that you can add objects to it). Then add the new data to this array using
[yourArray addObject:someObject];
Then after adding all objects write this array into the file.
Upvotes: 1