私はストーリーボードを使用してアプリケーションを構築しています。私はユーザーがツール(UILabels、画像...)のセットを持っているビューコントローラを持って、特定のボタンをクリックしてこれらの項目を追加することができます。すべてのデータが失われても、View Controllerを終了するかアプリケーションを終了します。私は、ボタンを押したときは、コードがトリガー(のNSLog)ですが、何も起こらないことをNSMutableDataとNSKeyedArchiverでUIObjectを保存
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
initWithTitle:@"Camera"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(useCamera:)];
UIBarButtonItem *cameraRollButton = [[UIBarButtonItem alloc]
initWithTitle:@"Camera Roll"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(useCameraRoll:)];
NSArray *items = [NSArray arrayWithObjects: cameraButton,
cameraRollButton, nil];
[toolbar setItems:items animated:NO];
mouseMoved = 0;
array = [[NSMutableArray alloc]init];
array2 = [[NSMutableArray alloc]init];
array3 = [[NSMutableArray alloc]init];
NSFileManager *filemgr; NSString *docsDir; NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex: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]) {
dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile: dataFilePath];
array = [dataArray objectAtIndex:0]; array2 = [dataArray objectAtIndex:1]; array3 = [dataArray objectAtIndex:2];
}}
-(void)saveData{
NSMutableArray *contactArray;
contactArray = [[NSMutableArray alloc] init]; [contactArray addObjectsFromArray:array]; [contactArray addObjectsFromArray:array2]; [contactArray addObjectsFromArray:array3]; [NSKeyedArchiver archiveRootObject:
contactArray toFile:dataFilePath];
[self.view addSubview: image1];
[self.view addSubview: text1];
[self.view addSubview: label1];
}
見ることができますので、私は、このようにボタンを保存の設定を試してみました。上記の例では、保存をクリックすると、ユーザーが作成したUILabel(alloc)が保存されます。また、アプリケーションが開いたときに、ボタンが保存されていればラベルは正確に同じ位置に配置されます。私はUIImageViewsとUITextFieldsでもthoを行う必要があります。
私はすべてのユーザ作成ラベルを...配列に追加したので、配列全体にオブジェクトを保存します。しかし、上記のコードは動作しません。助けを事前に
CGRect labelFrame = CGRectMake(400, 100, 100, 30);
label1 = [[UILabel alloc] initWithFrame: labelFrame];
[label1 setText: @"Untitled"];
[label1 setTextColor: [UIColor orangeColor]];
[self.view addSubview:label1];
label1.backgroundColor = [UIColor clearColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
[label1 addGestureRecognizer:pinchGesture];
if (array == nil) array = [[NSMutableArray alloc]init];
[array addObject:label1];
感謝を!!;私は(.hの中で、私はNSMutableArrayの*の配列を追加しました)私は、配列やオブジェクトを作成する方法であるwhy.this理解することはできません
これは動作しますが、最後に作成されたオブジェクトに対してのみ動作しますが、すべてのオブジェクトを残しておく必要があります。私はMVCを見てみましたが、その唯一のモデルです。それを行うより効率的な方法ですか?ホールビューコントローラを保存できますか? – Alessandro