Comment faire pour enregistrer des données avec NSUserDefaults en utilisant les données de NSMutableArray
Mon NSMutableArray
est déclaré dans mon .h fichier comme ceci
@property (strong, nonatomic) NSMutableArray * numbers;
Comment puis-je utiliser le NSMutableArray pour enregistrer les données d'inscription pour enregistrer à NSUserDefaults
Toute aide est très appréciée
Merci d'avance
code .h fichier
@property (retain, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray * numbers;
code .m
@synthesize myTableView, numbers;
-(void) viewDidLoad
{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:numbers forKey:@"numberArray"];
NSMutableArray *resultArray = [NSMutableArray arrayWithArray:[defaults objectForKey:@"numberArray"]];
[super viewDidLoad];
//instantiate our NSMutableArray
numbers = [[NSMutableArray alloc]initWithObjects:@"",@"", nil];
//Add a edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//Add the Add button
UIBarButtonItem * addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target: self action: @selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
}
-(void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[myTableView setEditing:editing animated:animated];
}
-(void) insertNewObject
{
//Display a UIAlertView
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Enter HomeWork" message: @"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
#pragma mark - UITableView Datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return numbers.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: cellIdentifier];
}
cell.textLabel.text = [numbers objectAtIndex:indexPath.row];
return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//remove our NSMutableArray
[numbers removeObjectAtIndex:indexPath.row];
//remove from our tableView
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - UITableView Delegate methods
#pragma mark - UIAlertViewDelegate Methods
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Only perform the following actions if the user hits the ok button
if (buttonIndex == 1)
{
NSString * tmpTextField = [alertView textFieldAtIndex:0].text;
if(!numbers)
numbers = [[NSMutableArray alloc]init];
[numbers insertObject:tmpTextField atIndex:0];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
@end
Ok, dans mon application, il y a une UITableView
lorsque l'utilisateur appuie sur le bouton "plus", un UIAlertView se présente avec un UITextField, lorsque l'utilisateur tape un mot, le texte va à la UITableView, mais lorsque l'utilisateur quitte l'application, le texte a disparu de la table. Je veux enregistrer le texte que l'utilisateur met en, de sorte que lorsque l'utilisateur quitte l'application et revient, je veux que le texte à enregistrer. Je veux l'enregistrer avec NSUserDefaults.
OriginalL'auteur user2167312 | 2013-03-13
Vous devez vous connecter pour publier un commentaire.
Selon vous, le scénario, lorsque vous exécutez une application après la sortie de... vous devriez en
viewDidLoad
vérifier si le keyExists dans leNSUserDefaults
puis récupérer les résultats dans le tableau et l'afficher dans laUITableView
. Pour sauvegarder les Données, c'est une bonne idée d'écouter une notification lorsque l'utilisateur de minimiser l'application, il suffit d'enregistrer les données dans les paramètres par défaut.Voici le code de travail pour votre cas
Merci pour correctement faire un nouveau mutable tableau, plutôt que de supposer que l'original mutable tableau serait encore mutable lorsque vous obtenez de retour 🙂
Merci à vous deux de vous 🙂
dois-je mettre cela dans la .m fichier, n'importe où?
est-il un endroit spécial, je suis suppose de mettre le code?
OriginalL'auteur nsgulliver