ios开发属性列表例子

来源:互联网 发布:天知地知你知我知 编辑:程序博客网 时间:2024/05/22 01:46



//前面有部分参考别人的代码

-(NSString *)dataFilePath
{
    

{
    
/*常量NSDocumentDirectory表明我们正在查找Documents目录路径,第二个常量NSUserDomainMask表示的是把搜索范围定在应用程序沙盒中,YES表示的是希望希望该函数能查看用户主目录*/
    
    
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
    
//  数组索引0Documentd目录,
    
NSString *documentDirectory = [paths objectAtIndex:0];
    
//    返回一个kFileName的完整路径
    
return [documentDirectory stringByAppendingPathComponent:@"mylist.plist"];//写自己喜欢的名字
}
- (
void)viewDidLoad
{
    [
super viewDidLoad];
    
    
    
//  检查数据文件是否存在,不存在则不加载
    
}

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

- (
IBAction)reshai:(id)sender {
    [
IDtest resignFirstResponder];
}
- (
IBAction)res:(id)sender {
    [
NameText resignFirstResponder];
}


- (
IBAction)save:(id)sender {
    
    
NSString *filePath = [self dataFilePath];//调用前面的方法
    
    
NSMutableArray *array=[[NSMutableArray alloc]init];
    
    
    [array 
addObject:IDtest.text];
    [array 
addObject:NameText.text];
    
    
    [array 
writeToFile:filePath atomically:YES];
    
     }

  
    
    
    


- (
IBAction)load:(id)sender {
    
    
NSString *filePath = [self dataFilePath];
    
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
        
NSArray *array = [[NSArray allocinitWithContentsOfFile:filePath];
        
        
IDtest.text = [array objectAtIndex:0];
        
NameText.text = [array objectAtIndex:1];
        
    }
    
//    注册一个通知,按下home键,执行applicationWillResignActive:方法
    
UIApplication *app = [UIApplication sharedApplication];
    [[
NSNotificationCenter defaultCenter]addObserver:self
                                            
selector:@selector(applicationWillResignActive:)
                                                
name:UIApplicationWillTerminateNotification
                                              
object:app];
    
}
@end
0 0
原创粉丝点击