从UITextVIEW保存文本?

时间:2010-10-22 09:24:35

标签: iphone ipad text save uitextview

我遇到了问题:我可以'找到一种方法在NSString中正确保存我的UITextView。 我创建了4个UITextViews,我希望它们保存在同一个文件中。 下面是代码:

-(void)destinataireTextView {

 self.destiView = [[[UITextView alloc] initWithFrame:CGRectMake(200, 160, 150, 120)] autorelease];
        [self.view addSubview: self.destiView];
}

-(void)expediteurTextView {

 self.expediView = [[[UITextView alloc] initWithFrame:CGRectMake(430, 200, 150, 120)] autorelease];
        [self.view addSubview: self.expediView];
}

-(void)objetTextView {

 self.objetView = [[[UITextView alloc] initWithFrame:CGRectMake(200, 295, 150, 120)] autorelease];
        [self.view addSubview: self.objetView];
}

-(void)corpsTextView {

 self.corpsView = [[[UITextView alloc] initWithFrame:CGRectMake(200, 335, 150, 120)] autorelease];
        [self.view addSubview: self.corpsView];
}

-(void)viewDidLoad{

 [super viewDidLoad];
        [self destinataireTextView];
 [self expediteurTextView];
 [self objetTextView];
 [self corpsTextView];
}

I've tried this piece of code :

-(void)viewDidLoad

[super viewDidLoad];

NSString *filePath = [self pathOfFile];

 if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {

  NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
  desti.text = [array objectAtIndex:0];
  [array release];
 }

 UIApplication *notif = [UIApplication sharedApplication];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTermite:) name:UIApplicationWillTerminateNotification object:notif];
}

-(NSString *) pathOfFile{

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentFolder = [paths objectAtIndex:0];
 return [documentFolder stringByAppendingFormat:@"SaveData.plist"];
}

-(void)applicationWillTermite:(NSNotification *)notification{

 NSMutableArray *save = [[NSMutableArray alloc] init];
 [save addObject:field1.text];
 [save writeToFile:[self pathOfFile]atomically:YES];
 [save release];
}

我迫切需要帮助。

THX

1 个答案:

答案 0 :(得分:2)

你真的没有给我们太多的帮助。没有错误消息。没有关于你尝试过什么或失败的建议。

然而,我的猜测是这一行:

return [documentFolder stringByAppendingFormat:@"SaveData.plist"];

我想你可能意味着:

return [documentFolder stringByAppendingPathComponent:@"SaveData.plist"];

否则你的文件名看起来像/some/pathSaveData.plist,显然是无效的。