-1

私の調査アプリの FirstViewController には、データ (文字列) を含む NSMutableArray があります。日付変数も作成しました。ここにコードがあります

NSDateFormatter *dateformater=[[NSDateFormatter alloc] init];
[dateformater setDateFormat:@"EEEE,dd MMMM yyyy HH:mm a"];

NSDate *today=[NSDate date];
NSString *currentDate=[dateformater stringFromDate:today];

今、私は NSUserDefaults の助けを借りて配列からデータを保存し、調査が行われたときに日付変数を保存し、それを SecondViewController のテーブルビューに表示します (最初のユーザーは調査の日付を表示し、次に日付をタップして配列からのデータを表示できます) )。

NSUSerDefaults の仕組みは知っていますが、SecondViewController に表示される日付変数ごとに配列を配置する方法がわかりません。誰でもそれについて私を助けることができますか?

4

2 に答える 2

0

ファイルでAppDelegate.h変数を宣言するだけです...

NSUserDefaults  *userDefaults;
NSMutableArray *arrDate;

後...

AppDelegate.mインフィルインapplicationDidFinishLonching:方式

    userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"arrDate"];
    if (dataRepresentingtblArrayForSearch != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingtblArrayForSearch];
        if (oldSavedArray != nil)
            arrDate = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            arrDate = [[NSMutableArray alloc] init];
    } else {
        arrDate = [[NSMutableArray alloc] init];
    }
    [arrDate retain];

その後、この UserDefaults からデータを挿入、更新、または削除する場合は、次のコードを使用します...

配列からレコードを削除する

[appDelegate.arrDate removeObjectAtIndex:Index];/// give the integer value instead of Index
    

これは配列にレコードを追加するためのものです..

[appDelegate.arrDate addObject:currentDate];///add value or add date here

最終的にこの変更を配列に保存した後、次のようにアーカイブします..

NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate.arrDate];
[appDelegate.userDefaults setObject:data forKey:@"arrDate"];
[appDelegate.userDefaults synchronize];
于 2012-11-07T11:09:46.180 に答える
0

NSDictionary日付とデータ オブジェクトを使用して を作成します。

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%@",currentDate], @"date", @"yourData", @"data", nil];

次に、それをNSUserDefault使用して入れます:

[NSUserDefaults standardUserDefaults] setObject:yourDictionary forKey:@"item1"];

これにより、単一のデータが NSUserDefault に保存されます。

以下を使用してオブジェクトを取得できます。

NSDictionary * dictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"item1"];

これで、次を使用して日付[dictionary objectForKey:@"date"];とデータを取得します。[dictionary objectForKey:@"data"];

于 2012-11-07T11:10:18.817 に答える