您的位置:首页 > 移动开发 > IOS开发

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!

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: