新风作浪博客学习(四)把plist里数据显示在textField上 .

在代码实现Lable 、textField创建界面以及键盘的处理一文中实现代码布局界面,前面也看了plist文件的一些操作,怎样把plist文件中的值显示在视图上呢,于是在代码实现Lable 、textField创建界面以及键盘的处理一文工程代码中添加了几行代码,让plist文件中的value显示在textField中;

1.打开工程之后,File --> New -->File 在弹出界面中左栏选中Mac os X 下的Resourse一栏,选中Property List,点击Next命名为testInfo.plist,然后打开testInfo.plist文件,在文件上右键Add Row,添加如下头所示数据(我用的Xcode版本是4.3.1,老版本的可能有的不一样)

[img]
[img]http://dl.iteye.com/upload/attachment/0079/3238/6bdfcca0-3908-3a95-be27-63d3c7fa07a4.png[/img]
[/img]


在View的最后面添加代码
//    读取plist文件
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"testInfo" ofType:@"plist"];

NSMutableDictionary *data=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
// 打印出plist文件
NSLog(@"%@",data);
//读取学生字典的内容到StudentInfo中qu
NSMutableDictionary *StudentInfo = [data objectForKey:@"Student"];
sNameTextField.text = [NSString stringWithFormat:@"%@",[StudentInfo objectForKey:@"Name"]];
sAgeTextField.text = [NSString stringWithFormat:@"%@",[StudentInfo objectForKey:@"Age"]];
sSexTextField.text = [NSString stringWithFormat:@"%@",[StudentInfo objectForKey:@"Sex"]];

NSMutableDictionary *teacherInfo = [data objectForKey:@"teacher"];
//在teacher字典中,把键为Name的值赋给tNameTextField的text上
tNameTextField.text = [NSString stringWithFormat:@"%@",[teacherInfo objectForKey:@"Name"]];
tSexTextField.text = [NSString stringWithFormat:@"%@",[teacherInfo objectForKey:@"Sex"]];



保存运行:
[img]
[img]http://dl.iteye.com/upload/attachment/0079/3240/3f3b54e8-b12f-38d2-ae9e-417506b5284b.png[/img]
[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值