`

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

    博客分类:
  • ios
ios 
阅读更多
在代码实现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]


在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]
  • 大小: 121.1 KB
  • 大小: 100.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics