代碼見 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
代碼見 http://download.csdn.net/detail/x1135768777/4197663