iOS如何在退出应用程序后将主视图中的addButton添加保存到主视图中

时间:2014-08-13 13:41:42

标签: ios uitableview

我有一个master-detail程序,我通过UIAlert将项目添加到主列表中,但是当应用程序退出时,我添加的项目消失了,如何保存我添加的值?我将如何更改以下代码来执行此操作?我听说你可以保存到plist,但我不知道如何使用该功能,我不确定它是否必要。谢谢!

我要保存和重新加载的值是" keepValue"这是我在头文件中定义的NSString属性。

如果您很好奇我如何将NSDate更改为弹出的AlertView以添加您想要的任何值:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                               target:self
                                                                               action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

//modified to pop up alertView see: http://stackoverflow.com/questions/11163341/how-do-i-replace-the-date-with-a-writable-title
- (void)insertNewObject:(id)sender
{
    UIAlertView *getTitle = [[UIAlertView alloc] initWithTitle:@"Add Search Keyword" message:nil delegate:self
                                             cancelButtonTitle:@"Add"
                                             otherButtonTitles:nil];
    getTitle.alertViewStyle = UIAlertViewStylePlainTextInput;
    [getTitle show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    NSString *  userEnterThisString = [[alertView textFieldAtIndex:0] text];
    [_objects insertObject:userEnterThisString atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    self.keepValue = userEnterThisString;  //Want to keep after app quits
    [self.savedSearchValues addObject:self.keepValue];  //trying to save values to reuse?

}

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

    NSString *object = _objects[indexPath.row]; //changed here from default NSDate
    cell.textLabel.text = [object description];
    return cell;
}

2 个答案:

答案 0 :(得分:0)

使用NSUserDefaults。 StackOverflow中有很多答案。

答案 1 :(得分:0)

我在另一个网站上得到了很好的回复,谢谢@Robert Bojor。对于其他好奇的人,我将保存数据添加到代码的文件部分到AlertView方法的末尾,我在viewDidLoad中添加了读回来(两者都在主视图中) 如果您有兴趣,这是最终代码:

- (void)viewDidLoad
{
[super viewDidLoad];

self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;

//**********************READ CODE*************************
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) {
NSString *addedItems = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"addedItems.dta"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:addedItems];
if (fileExists) {
    _objects = [NSArray arrayWithContentsOfFile:addedItems];
} else {
    // Notify the user the file doesn't exist or try to load it from a cached resource.
}
}

}

//modified to pop up alertView see: http://stackoverflow.com/questions/11163341/how-do-i-replace-the-date-with-a-writable-title

- (void)insertNewObject:(id)sender {
UIAlertView *getTitle = [[UIAlertView alloc] initWithTitle:@"Add Search Keyword" message:nil delegate:self cancelButtonTitle:@"Add" otherButtonTitles:nil];
getTitle.alertViewStyle = UIAlertViewStylePlainTextInput;
[getTitle show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
NSString *  userEnterThisString = [[alertView textFieldAtIndex:0] text];
[_objects insertObject:userEnterThisString atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
self.keepValue = userEnterThisString;


//**********************SAVE CODE*************************
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *addedItems = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"addedItems.dta"];
// The file name and extension can be changed at will
[_objects writeToFile:addedItems atomically:YES];

}