7.20

2016-07-20  本文已影响0人  zhujian6

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.
}
//事件驱动
func didClick() {
    let guess = UInt32(textField.text!)
print("输出结果是:", 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 = "对了" }
}

}

上一篇下一篇

猜你喜欢

热点阅读