用swift写了一个生肖查询的例子,记录下以后可以看看
先看界面:
类似这样的界面 ,我用的是sb搭建的界面
拖控件,然后关联事件和属性,代码如下:
import UIKit
class ViewController: UIViewController {
let arry = ["shu","niu","hu","tu","long","she","ma","yang","hou","ji","gou","zhu"]
@IBOutlet var inputTextFiled: UITextField!
@IBOutlet var shuxingLabel: UILabel!
@IBAction func respondsToSureBtn(sender: UIButton) {
let text:Int = Int(inputTextFiled.text!)!
shuxingLabel.text = returnNumber(text)
}
func returnNumber(a: Int) -> String {
if a>1900 {
let b = (a - 1900)%12
return arry[b]
}else{
return String();
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}