import UIKit
class ViewController: UIViewController {
var result = arc4random_uniform(100)
var label: UILabel!
let textField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let rect = CGRectMake(100, 100, 200, 100)
label = UILabel(frame: rect)
label.text = "你好"
label.backgroundColor = UIColor.redColor()
label.textColor = UIColor.greenColor()
label.font = UIFont.boldSystemFontOfSize(28)
label.textAlignment = .Center
self.view.addSubview(label)
// Do any additional setup after loading the view, typically from a nib.
//文本框谎替,只能輸入單行文字
// let textField = UITextField()
textField.frame = CGRect(x: 150, y: 250, width: 100, height: 50)
textField.borderStyle = .Bezel
textField.placeholder = "請在這里輸入文字"
textField.textColor = UIColor.blueColor()
textField.keyboardType = .NumbersAndPunctuation
textField.clearsOnInsertion = true
self.view.addSubview(textField)
let btn = UIButton(type: .System)
btn.setTitle("確認", forState: .Normal)
// btn.setTitle("高亮", forState: .Highlighted)
// btn.setTitle("選中", forState: .Selected)
// btn.selected = true
// btn.setTitle("禁用", forState: .Disabled)
// btn.enabled = false
let image = UIImage(named: "check")
// btn.setImage(image, forState: .Normal)
btn.setBackgroundImage(image, forState: .Normal)
btn.frame = CGRect(x: 150, y: 350, width: 100, height: 100)
btn.addTarget(self, action: #selector(didClick), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//事件驅(qū)動
func didClick() {
let guess = UInt32(textField.text!)
print("輸出結(jié)果是:", textField.text)
if guess > 100 { label.text = "再輸入"}
else if guess > result {
label.text = "大了"}
else if guess < result
{ label.text = "小了"}
else if guess == result { label.text = "對了" }
}
}