(스토리 보드 이미지) http://i.stack.imgur.com/DUZ12.png주소록 데이터 지속성?
사용자가 데이터를 입력하고 저장하는 3 개의 텍스트 필드가 있습니다. 응용 프로그램을 열 때 저장 데이터가 있으면 이전 입력이 텍스트 필드 내에 표시됩니다. 문제는 하나의 데이터 집합 만 있지만 여러 사람의 정보가있는 배열이어야한다는 것입니다. 대신 이름이있는 셀이있는 내비게이션 컨트롤러를 만들고 관련 콘택트 정보를 표시하는 데 클릭하면됩니다.
@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
viewcontroller.h는 시간 내 주셔서 감사합니다.
호기심에서 벗어나서 주소록과 유사한 앱을 만드는 방법이 좋은 접근이라고 생각합니까? –
전체 구조에 대해 자세히 설명하지 않았습니다. 그것은 네비게이션 컨트롤러에서 테이블 뷰일 것입니다. iPad에서 좀 더 흥미로운 인터페이스를 제공하기 위해 컬렉션보기를 사용할 수 있습니다 ... – Wain