2011-09-21 48 views
0
#import <UIKit/UIKit.h> 

@interface SavingDataViewController : UIViewController { 

    UITextField *field1; 

} 
@property (nonatomic , retain) IBOutlet UITextField *field1; 

-(NSString *) pathOfFile; 
-(void) applicationWillTerminate:(NSNotification *) notification; 


@end 




@implementation SavingDataViewController 
@synthesize field1; 

-(NSString *) pathOfFile 
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *doucumentFolder = [path objectAtIndex:0]; 
    return [doucumentFolder stringByAppendingFormat:@"myFile.plist"]; 

} 
-(void) applicationWillTerminate:(NSNotification *) notification 
{ 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [array addObject:field1.text]; 
    [array writeToFile:[self pathOfFile] atomically:YES]; 
    [array release]; 

} 


- (void)viewDidLoad 
{ 
    NSString *filePath = [self pathOfFile]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    { 
     NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
     field1.text = [array objectAtIndex:0]; 
     [array release]; 
    } 

    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app]; 



    [super viewDidLoad]; 
} 

我想節省的plist文件中的數據與此代碼,但是當我重新啓動程序,我在這行的問題: INT retVal的= UIApplicationMain(ARGC,ARGV ,零,零);有錯誤:程序收到信號如何在plist文件數據保存在可可觸摸

什麼我的問題呢?

回答

0

它確實保存文件?!當您手動打開文件時,文件中有什麼。 您可能希望通過使用stringByAppendingPathComponent而不是stringByAppendingFormat來連接文檔路徑,因爲我認爲文檔路徑沒有尾隨路徑分隔符。