0

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

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

我尝试添加一个名为 SaveValue 的按钮并将其编码如下:

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

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

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

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

我希望有一个人可以帮助我。谢谢。

编辑:

-(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];
}
4

1 回答 1

1

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

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

例如

#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 来保存数据,因为用户默认值通常用于保存某种设置(但对于某些“文本”,它应该完全可以工作出去)。

// 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.

于 2013-03-30T17:45:42.997 回答