Advertisement
zxlk21e

nsuserdefaultsissue

May 22nd, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. //
  2. // SavedTableViewController.m
  3. //
  4.  
  5. #import "SavedTableViewController.h"
  6.  
  7. @interface SavedTableViewController ()
  8.  
  9. @end
  10.  
  11. @implementation SavedTableViewController
  12. @synthesize removeAll;
  13. - (IBAction)removeAll:(id)sender {
  14. [[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
  15.  
  16. }
  17.  
  18. - (id)initWithStyle:(UITableViewStyle)style
  19. {
  20. self = [super initWithStyle:style];
  21. if (self) {
  22. // Custom initialization
  23. }
  24. return self;
  25. }
  26. - (void)viewWillAppear:(BOOL)animated {
  27. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  28. listOfNames = [[defaults objectForKey:@"My Key"] copy];
  29. [self.tableView reloadData];
  30. }
  31.  
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35.  
  36. //Get Defaults
  37. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  38. listOfNames = [[defaults objectForKey:@"My Key"] copy];
  39.  
  40. //Set the title
  41. self.navigationItem.title = @"Saved Baby Names";
  42.  
  43. // Uncomment the following line to preserve selection between presentations.
  44. // self.clearsSelectionOnViewWillAppear = NO;
  45.  
  46. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  47. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  48. }
  49.  
  50. - (void)viewDidUnload
  51. {
  52. [self setRemoveAll:nil];
  53. [super viewDidUnload];
  54.  
  55. // Release any retained subviews of the main view.
  56. // e.g. self.myOutlet = nil;
  57. }
  58.  
  59. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  60. {
  61. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  62. }
  63.  
  64. #pragma mark - Table view data source
  65.  
  66. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  67. {
  68. // Return the number of sections.
  69. return 1;
  70. }
  71.  
  72. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  73. {
  74. // Return the number of rows in the section.
  75. return [listOfNames count];
  76. }
  77.  
  78. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. static NSString *CellIdentifier = @"Cell";
  81. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  82. if (cell == nil) {
  83. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  84. }
  85.  
  86. // Set up the cell...
  87. NSString *cellValue = [listOfNames objectAtIndex:indexPath.row];
  88. [[cell textLabel] setText: cellValue ];
  89.  
  90. return cell;
  91. }
  92.  
  93.  
  94. /*
  95. // Override to support conditional editing of the table view.
  96. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98. // Return NO if you do not want the specified item to be editable.
  99. return YES;
  100. }
  101. */
  102.  
  103.  
  104. // Override to support editing the table view.
  105. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. if (editingStyle == UITableViewCellEditingStyleDelete) {
  108.  
  109. // edit list of names
  110. if ([listOfNames count] >= 1) {
  111. [tableView beginUpdates];
  112. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  113. [listOfNames removeObjectAtIndex:[indexPath row]];
  114.  
  115. // write updated listofnames to nsuserdefaults
  116. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  117.  
  118. [[NSUserDefaults standardUserDefaults] setObject:listOfNames forKey:@"My Key"];
  119.  
  120. [defaults synchronize];
  121.  
  122. if ([listOfNames count] == 0) {
  123. [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  124. }
  125. [tableView endUpdates];
  126. }
  127.  
  128.  
  129. }
  130. else if (editingStyle == UITableViewCellEditingStyleInsert) {
  131. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  132. }
  133. }
  134.  
  135.  
  136. /*
  137. // Override to support rearranging the table view.
  138. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  139. {
  140. }
  141. */
  142.  
  143. /*
  144. // Override to support conditional rearranging of the table view.
  145. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. // Return NO if you do not want the item to be re-orderable.
  148. return YES;
  149. }
  150. */
  151.  
  152. #pragma mark - Table view delegate
  153.  
  154. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. // Navigation logic may go here. Create and push another view controller.
  157. /*
  158. <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
  159. // ...
  160. // Pass the selected object to the new view controller.
  161. [self.navigationController pushViewController:detailViewController animated:YES];
  162. */
  163. }
  164.  
  165. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement