IOS 使用XML制作简易选择题
2015-10-07 16:50
519 查看
//# ViewController.Swift import UIKit class ViewController: UIViewController,NSXMLParserDelegate { @IBOutlet var lQuestion: UILabel! @IBOutlet var bntsubmit: UIButton! @IBOutlet var InputAnwser: UITextField! @IBOutlet var lAnwserD: UILabel! @IBOutlet var lAnwserC: UILabel! @IBOutlet var lAnwserB: UILabel! @IBOutlet var lAnwserA: UILabel! var questions:Array<Question> = [] //定义一个数组 var currentQuestion:Question! //定义一个Question类型变量 var currentShowQuestion:Question! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let parser = NSXMLParser(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Questions", ofType: "xml")!)) parser?.delegate = self parser?.parse() } func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { if elementName == "question"{ //解析到question 节点的处理事件 currentQuestion = Question() questions.append(currentQuestion) //添加到数组里面 //把问题节点里面的text属性绑定给currentQuestion.question currentQuestion.question = attributeDict["text"]! as String //把问题节点里面的right属性绑定给currentQuestion.right currentQuestion.right = attributeDict["right"]! as String }else if elementName == "anwser"{ //解析到 anwser 节点的处理事件 let tag = attributeDict["tag"]! as String if tag == "A"{ //标签为A则显示 A标签属性text内容 currentQuestion.anwserA = attributeDict["text"]! as String }else if tag == "B"{//标签为B则显示 B标签属性text内容 currentQuestion.anwserB = attributeDict["text"]! as String }else if tag == "C"{//标签为C则显示 C标签属性text内容 currentQuestion.anwserC = attributeDict["text"]! as String }else if tag == "D"{//标签为D则显示 D标签属性text内容 currentQuestion.anwserD = attributeDict["text"]! as String } } } @IBAction func bntpress(sender: AnyObject) { //判断 输入的是否与正确答案相同 //若输入小写,则赋值其对应的大写 var input = InputAnwser.text if input == "a"{ input = "A" }else if input == "b"{ input = "B" }else if input == "c"{ input = "C" }else if input == "d"{ input = "D" } if input == currentShowQuestion.right{ print("正确") }else{ print("错误") } } var x = 0 func parserDidEndDocument(parser: NSXMLParser) { // print("Size of array:\(questions.count)") currentShowQuestion = questions[x] lQuestion.text = currentShowQuestion.question lAnwserA.text = currentShowQuestion.anwserA lAnwserB.text = currentShowQuestion.anwserB lAnwserC.text = currentShowQuestion.anwserC lAnwserD.text = currentShowQuestion.anwserD } @IBAction func xiayiti(sender: AnyObject) { if x==2{ print("没题啦") }else{ x++ currentShowQuestion = questions[x] lQuestion.text = currentShowQuestion.question lAnwserA.text = currentShowQuestion.anwserA lAnwserB.text = currentShowQuestion.anwserB lAnwserC.text = currentShowQuestion.anwserC lAnwserD.text = currentShowQuestion.anwserD } } @IBAction func shangyiti(sender: AnyObject) { if x==0{ print("没题啦") }else{ x-- currentShowQuestion = questions[x] lQuestion.text = currentShowQuestion.question lAnwserA.text = currentShowQuestion.anwserA lAnwserB.text = currentShowQuestion.anwserB lAnwserC.text = currentShowQuestion.anwserC lAnwserD.text = currentShowQuestion.anwserD } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
//# Questions.xml <questions> <question text="1+1=?" right="C"> <anwser text="1" tag="A"></anwser> <anwser text="0" tag="B"></anwser> <anwser text="2" tag="C"></anwser> <anwser text="3" tag="D"></anwser> </question> <question text="1+0=?" right="A"> <anwser text="1" tag="A"></anwser> <anwser text="0" tag="B"></anwser> <anwser text="2" tag="C"></anwser> <anwser text="3" tag="D"></anwser> </question> <question text="1+2=?" right="D"> <anwser text="1" tag="A"></anwser> <anwser text="0" tag="B"></anwser> <anwser text="2" tag="C"></anwser> <anwser text="3" tag="D"></anwser> </question> </questions>
//#Question.swift import UIKit class Question: NSObject { var question:String! var right:String! var anwserA:String! var anwserB:String! var anwserC:String! var anwserD:String! }
相关文章推荐
- iOS新手入门之点击空白处回收键盘
- iOS ----Block语法
- iOS开发关闭NSLog在控制台不能够输出
- iOS-Quartz 2D
- iOS 经典笔记
- iOS-真机调试
- iOS-AFN相较ASI
- iOS数据持久化
- iOS-ASI
- iOS开发-XIB、Storyboard操作小技巧
- iOS开发零基础教程之真机调试流程
- iOS开发- 处理空字符串
- autoresizesSubviews
- IOS开发——多线程
- 如何把设计图自动转换为iOS代码? 在线等,挺急的!
- iOS中使用正则表达式去掉HTML中的标签元素获得纯文本的方法
- IOS使用XMPP最新教程(四)获取好友列表 (通过花名册)
- AVAudioSession、AVAudioRecorder、AVAudioPlayer
- iOS--关于AFN和ASI的post登录请求
- IOS开发笔记-01按钮操作-12.frame&bounds&center属性