0

(絵コンテ画像) http://i.stack.imgur.com/DUZ12.png

ユーザーがデータを入力して保存する 3 つのテキスト フィールドを用意します。アプリケーションを開くと、セーブデータがあれば、テキストフィールド内に以前の入力が表示されます。問題は、複数の人の情報を含む配列である必要があるのに、データのセットが 1 つしかないことです。代わりに、名前の付いたセルを持つナビゲーション コントローラーを作成したいと思います。それらをクリックすると、関連する連絡先情報が表示されます。

viewcontroller.h

   @interface ArchiveViewController : UIViewController
    @property (strong, nonatomic) IBOutlet UITextField *name;
    @property (strong, nonatomic) IBOutlet UITextField *address;
    @property (strong, nonatomic) IBOutlet UITextField *phone;
    @property (strong, nonatomic) NSString *dataFilePath;
    - (IBAction)saveData:(id)sender;
    @end

viewcontroller.m

@interface ArchiveViewController ()

@end

@implementation ArchiveViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
 NSFileManager *filemgr;
 NSString *docsDir;
 NSArray *dirPaths;

 filemgr = [NSFileManager defaultManager];

 // Get the documents directory
 dirPaths = NSSearchPathForDirectoriesInDomains(
   NSDocumentDirectory, NSUserDomainMask, YES);

 docsDir = dirPaths[0];

 // Build the path to the data file
 _dataFilePath = [[NSString alloc] initWithString: [docsDir
        stringByAppendingPathComponent: @"data.archive"]];

 // Check if the file already exists
 if ([filemgr fileExistsAtPath: _dataFilePath])
 {
         NSMutableArray *dataArray;

         dataArray = [NSKeyedUnarchiver
          unarchiveObjectWithFile: _dataFilePath];

         _name.text = dataArray[0];
         _address.text = dataArray[1];
         _phone.text = dataArray[2];
 }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)saveData:(id)sender {
     NSMutableArray *contactArray;

     contactArray = [[NSMutableArray alloc] init];
     [contactArray addObject:self.name.text];
     [contactArray addObject:self.address.text];
     [contactArray addObject:self.phone.text];
     [NSKeyedArchiver archiveRootObject: 
       contactArray toFile:_dataFilePath];

}
@end

お時間をいただきありがとうございます。

4

1 に答える 1

0

3 つのテキスト要素を含む配列を作成して を使用する代わりに、NSKeyedArchiver辞書を含む配列を作成して で保存しwriteToFile:atomically:ます。これにより、配列がフィールドのリストではなく「エントリ」のリストとして使用され、データがバイナリ ファイルではなく plist に保存されます。

ここで、配列を読み込むと、エントリのテーブル ビューを表示できます (名前だけを表示するなど)。次に、アーカイブ ビューを表示するときに、コントローラーに適切な辞書を渡します。

保存するには、委任を使用して編集内容をマスター コントローラーに戻すとよいでしょう。ただし、直接 (詳細コントローラーの知識が必要) または通知によって行うこともできます。

于 2013-07-20T21:34:16.030 に答える