Xcode 使用 NSUserDefaults 在主视图控制器上保存数据 [英] Xcode using NSUserDefaults to save data on Master View Controller

查看:60
本文介绍了Xcode 使用 NSUserDefaults 在主视图控制器上保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 ipad 创建一个主详细信息应用程序,它应该保存用户在主视图控制器的表视图 cel 中输入的任何内容(使用弹出视图上的按钮,在该弹出视图中的标签上放置一些文本).

I am creating a master detail app for the ipad that should save whatever the user is typing (using buttons on the popover view which puts some text on the label in that popover view) in the table view cel in master view controller.

所以基本上我有一个弹出视图,其中包含一个名为 inputDisplayLabel 的 UILabel 以及用户按下的任何按钮,该按钮的文本都在该 UILabel 中.当按下输入按钮时,它会将该文本添加到 tableviews cel.现在我想保存该表格视图中的任何内容,以便当我退出应用程序并重新开始时,文本仍保留在表格视图中.

So basically I have a popover view that contains a UILabel called inputDisplayLabel and whatever button the user presses that buttons text goes in that UILabel. When enter button is pressed it adds that text to the tableviews cel. Now I want to save whatever is in that table view sothat when I quit the application and start over again the text still stays in the tableview cel.

我尝试添加一个名为 SaveValue 的按钮并对其进行编码:

I tried to add a button called SaveValue and coded it lik so:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.inputDisplayLabel.text forKey:@"savedValue"];
[defaults synchronize];

现在我尝试使用以下代码添加另一个名为 loadValue 的按钮来加载它:

now I tried loading it with adding another button called loadValue with the following code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.tableView = [defaults objectForKey:@"SavedValue"];

但它不起作用.我实际上将加载代码放在哪里,以便在应用程序启动时它就在那里,这样我就不必使用另一个按钮.

but it doesnt work. Where do I actually put the loading code so that it is there when the application starts so that I dont have to use another button.

我希望有人可以帮助我.谢谢.

I hope someone can help me. Thanks.

-(void)setTheTextOfLabelWith:(NSString *)str
{
    text = str;
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    //[_objects insertObject:[NSDate date] atIndex:0];
    [_objects insertObject:[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];


    [self.inputViewPopover dismissPopoverAnimated:YES];
}

推荐答案

Objective C 区分大小写savedValue"与SavedValue"不同.当然,tableview 不能直接取字符串.您必须将字符串/文本分配给例如tableviewcell 中的标签.将代码放在 viewDidLoad 中,因此将标签的文本设置在那里,如果它在 tableViewCell 中,则必须在cellForRowAtIndexPath"方法中设置它.也许应该看看 viewcontroller 生命周期以及 UITableViewDelegate 和 DataSource 协议.

Objective C is case sensitive "savedValue" is different to "SavedValue". And of course the tableview can't directly take a string. You have to assign the string/text to e.g. a label in a tableviewcell. Put the code in viewDidLoad, so set the label's text there, if it is in a tableViewCell you will have to set it in "cellForRowAtIndexPath" method. Maybe should take a look at the viewcontroller lifecycle and the UITableViewDelegate and DataSource protocols.

//我不这么认为......基本上你有一个动态的tableview并且想要多个单元格显示你保存在用户默认值中的一些文本?要拥有动态 tableview,您通常在 ViewController 中实现 UITableViewDataSource 和 UITableViewDelegate 协议,它显示/管理 tableview.在那里您使用 UITableViewDataSource 协议中的cellForRowAtIndexPath"方法创建单元格.当您想保存多个条目时,您可以使用数组将所有内容保存在用户默认值中,然后在 cellForRow 中对其进行迭代......以获取单元格中的所有文本.

// edit: I don't think so.. basically you have a dynamic tableview and want multiple cells displaying some text you saved in user defaults? To have a dynamic tableview, you normally implement the UITableViewDataSource and UITableViewDelegate protocols in your ViewController, which displays/manages the tableview. There you create the cells using the "cellForRowAtIndexPath" method from the UITableViewDataSource protocol. As you want to save more than one entry you could use an array to save everything in user defaults and just iterate it in cellForRow.. to get all the texts in the cells.

例如

#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 self.yourArray.count;
}

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

    if(theCell == nil)
    {
        theCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:CellIdentifier];
    }

    theCell.textLabel.text = [self.yourArray objectAtIndex:indexPath.row];

    return theCell;
}

如果你真的想保存大量数据,考虑使用 CoreData 或某种 XML 来保存数据,因为用户默认值通常用于保存某种设置(但对于一些文本"它应该完全可行).

If you want to save really a lot of data, think about CoreData or some kind of XML to save the data, as the user defaults are commonly rather used to save some kind of settings (but for just some "texts" it should totally work out).

//编辑 2:检查您的项目后,我注意到您尝试将标签保存在 NSUserDefaults 中,这是不可能的.你必须保存这些标签的文本,然后在从 NSUserDefaults 加载它们后用这些文本恢复标签.最简单的解决方案(或至少没有太多修改的解决方案)是像这样修改加载和保存方法:

// edit2: After checking out your project, I noticed that you tried to save the labels in NSUserDefaults, which is not possible. You have to save the texts of these labels and then restore the labels with these texts after loading them from NSUserDefaults. The easiest solution (or at least the one without many modifications) is to modify your load and save methods like so:

- (IBAction)load
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSArray *texts = [defaults objectForKey:@"items"];
    for(NSString *currentText in texts)
    {
        [self setTheTextOfLabelWith:currentText];
    }
}

- (void)savingData
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSMutableArray *textToSave = [[NSMutableArray alloc] init];
    for(UILabel *currentLabel in _objects)
    {
        [textToSave addObject:currentLabel.text];
    }

    [defaults setObject:textToSave forKey:@"items"]; 
    [defaults synchronize];
}

您也可以在回复邮件时找到该解决方案.

You can also find that solution as a reply to your mail.

这篇关于Xcode 使用 NSUserDefaults 在主视图控制器上保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆