0

マスタービューコントローラーのテーブルビューセルに、ユーザーが入力しているものを保存するiPad用のマスター詳細アプリを作成しています(ポップオーバービューのボタンを使用して、そのポップオーバービューのラベルにテキストを配置します)。

したがって、基本的には、inputDisplayLabel と呼ばれる UILabel を含むポップオーバー ビューがあり、ユーザーがそのボタンのテキストを押すボタンはすべて、その UILabel に入ります。Enter ボタンを押すと、そのテキストが tableviews セルに追加されます。今、そのテーブル ビューにあるものを保存して、アプリケーションを終了して最初からやり直したときに、テキストがテーブルビュー セルに残るようにしたいと考えています。

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」とは異なります。もちろん、テーブルビューは文字列を直接受け取ることはできません。テーブルビューセルのラベルなどに文字列/テキストを割り当てる必要があります。コードをviewDidLoadに入れて、そこにラベルのテキストを設定します.tableViewCellにある場合は、「cellForRowAtIndexPath」メソッドで設定する必要があります。ビューコントローラーのライフサイクルと、UITableViewDelegate および DataSource プロトコルを確認する必要があるかもしれません。

//編集:そうは思いません..基本的に、動的なテーブルビューがあり、ユーザーのデフォルトに保存したテキストを複数のセルに表示したいですか?動的なテーブルビューを作成するには、通常、テーブルビューを表示/管理する ViewController に UITableViewDataSource および UITableViewDelegate プロトコルを実装します。そこで、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: プロジェクトをチェックアウトした後、ラベルを NSUserDefaults に保存しようとしたことに気付きましたが、これは不可能です。これらのラベルのテキストを保存し、NSUserDefaults からロードした後、これらのテキストでラベルを復元する必要があります。最も簡単な解決策 (または少なくとも多くの変更を伴わない解決策) は、load メソッドと save メソッドを次のように変更することです。

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

メールへの返信として、その解決策を見つけることもできます。

于 2013-03-30T17:45:42.997 に答える