代码见 http://download.csdn.net/detail/x1135768777/4197663
在.h文件内声明
NSMutableDictionary* dict;
UITextView* textView;
在.m文件内
//
// PlistViewController.m
// Plist read and write
//
// Created by wang doublejie on 12-3-31.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "PlistViewController.h"
@implementation PlistViewController
#define PATH [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]
@synthesize textView,dict;
-(void)viewDidLoad
{
[superviewDidLoad];
dict = [[NSMutableDictionaryalloc ]initWithContentsOfFile:PATH];//文件位置
NSMutableDictionary* item= [dictobjectForKey:@"item1"];//第一层 NSMutableDictionary
NSMutableDictionary* page= [itemobjectForKey:@"page1"];//第二层NSMutableDictionary
//plist里面想读取的值的上面有几个NSMutableDictionary就要几个NSMutableDictionary来解读
NSString *content = [page objectForKey:@"content"]; //内容
NSString *position = [page objectForKey:@"content_position"]; //文字位置
NSArray *arrPos =[positioncomponentsSeparatedByString:NSLocalizedString(@",",nil)];//位置数组拆分
textView=[[UITextViewalloc]initWithFrame:CGRectMake([[arrPosobjectAtIndex:0]floatValue], [[arrPosobjectAtIndex:1]floatValue], [[arrPosobjectAtIndex:2]floatValue], [[arrPosobjectAtIndex:3]floatValue])] ;
// [arrPos objectAtIndex:3]数组中第三个数值[[arrPos objectAtIndex:1] floatValue]是将读出的NSString转换成NSString;
textView.text=content;//将读出的内容赋值给text;
// textView.userInteractionEnabled = NO;取消文本复制
textView.editable=NO;
[self.viewaddSubview:textView];//添加视图上
NSString* item2= [dictobjectForKey:@"item1"];//读取item2
NSLog(@"%@",item2);
NSString* page_num= [item objectForKey:@"page_num"]; //读取page_num
NSLog(@"%@",page_num);
NSArray* arr=[dictobjectForKey:@"array"];
for(int i=0,j=2;i<=5;i++,j++)
{
//NSString* str=[[NSString alloc]initWithFormat:@"item%d",i];
NSString* item= [arr objectAtIndex:i];
textView=[[UITextViewalloc]initWithFrame:CGRectMake(90, 50*j, 70, 20)];
textView.text=item;
textView.editable=NO;
[self.viewaddSubview:textView];//添加视图上
}
for(int count=2;count<[dictcount];count++)//count是其保存的数目
{
NSString* str=[[NSStringalloc]initWithFormat:@"item%d",count];
NSString* item= [dictobjectForKey:str];
textView=[[UITextViewalloc]initWithFrame:CGRectMake(150, 50*count, 70, 20)];
textView.text=item;
textView.editable=NO;
[self.viewaddSubview:textView];//添加到视图上
}
UIButton *addbtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
addbtn.frame = CGRectMake(0, 60, 90, 35);
[addbtn setTitle:@"添加"forState:UIControlStateNormal];
[addbtn setTitle:@"添加"forState:UIControlStateHighlighted];
[addbtn addTarget:selfaction:@selector(add:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:addbtn];//添加到视图上
UIButton *editbtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
editbtn.frame = CGRectMake(0, 160, 90, 35);
[editbtn setTitle:@"修改"forState:UIControlStateNormal];
[editbtn setTitle:@"修改"forState:UIControlStateHighlighted];
[editbtn addTarget:selfaction:@selector(edit:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:editbtn];//添加到视图上
UIButton *delbtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
delbtn.frame = CGRectMake(0, 260, 90, 35);
[delbtn setTitle:@"删除"forState:UIControlStateNormal];
[delbtn setTitle:@"删除"forState:UIControlStateHighlighted];
[delbtn addTarget:selfaction:@selector(del:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:delbtn];//添加到视图上
}
-(IBAction)add:(id)sender
{
[dictsetObject:@"item8"forKey:@"item8"];//添加更修改方法相同,他是先查找是否存在该项,如果存在修改该项,如果不存在直接添加
[dict writeToFile:PATHatomically:YES]; //保存到plist里
[selfviewDidLoad];
}
-(IBAction)edit:(id)sender
{
[dict setObject:@"hello"forKey:@"item2"];//修改
[dict writeToFile:PATHatomically:YES];
[selfviewDidLoad];
}
- (IBAction)del:(id)sender
{
[dict removeObjectForKey:@"item5"];//删除
[dict writeToFile:PATHatomically:YES];
[selfviewDidLoad];
}
@end