退出应用程序后,表视图保存用户输入数据

时间:2014-04-23 07:40:15

标签: ios uitableview

我有一个使用表视图控制器的应用程序。我能够在表格视图中保存和显示数据,但是一旦我退出应用程序,它就会丢失信息并且表格变空。我是iOS开发的新手,任何帮助都将受到高度赞赏。

我添加了 AddReminderViewController RemindersTableViewController 文件。我真的很陌生,所以请你告诉我我必须添加的代码aplicationDidEnterBackground'保存数据。谢谢所有那些已经如此迅速地帮助过我的人。

AddReminderViewController.h

#import <UIKit/UIKit.h>

@interface AddReminderViewController : UIViewController<UITextFieldDelegate>

- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;


@property (weak, nonatomic) IBOutlet UITextField *itemText;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;

@end

AddReminderViewController.m

#import "AddReminderViewController.h"

@interface AddReminderViewController ()

@end

@implementation AddReminderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.itemText.delegate = self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}




- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(id)sender {
[self.itemText resignFirstResponder];

// Get the current date
NSDate *pickerDate = [self.datePicker date];

// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]   applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.itemText resignFirstResponder];
return NO;
}

@end

RemindersTableViewController.h

#import <UIKit/UIKit.h>

@interface RemindersTableViewController : UITableViewController

@end

RemindersTableViewController.m

#import "RemindersTableViewController.h"


@interface RemindersTableViewController ()
- (void)reloadTable;

@end

@implementation RemindersTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}


- (void)viewDidLoad
{
[super viewDidLoad];


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reloadTable)
                                             name:@"reloadData"
                                           object:nil];
 }



- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier     forIndexPath:indexPath];

// Get list of local notifications
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *localNotification = [localNotifications objectAtIndex:indexPath.row];

// Display notification info
[cell.textLabel setText:localNotification.alertBody];
[cell.detailTextLabel setText:[localNotification.fireDate description]];

return cell;
}



- (void)reloadTable
{
   [self.tableView reloadData];
}




#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}

@end

2 个答案:

答案 0 :(得分:0)

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers,     and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

答案 1 :(得分:0)

是的,您可以使用- (void)applicationDidEnterBackground:(UIApplication *)application- (void)applicationWillTerminate:(UIApplication *)application之一来保存数据。

您可以在- (void)applicationDidBecomeActive:(UIApplication *)application

中获取数据