Swift UI学习之基础:UIButton,UILabel and UISlider

//
//  RootViewController.swift
//  UIDemos
//
//  Created by sixiaobo on 14-6-10.
//  Copyright (c) 2014年 com.huangyibiao. All rights reserved.
//

import Foundation
import UIKit

class RootViewController : UIViewController, UIAlertViewDelegate {
    var _label: UILabel? // optional type, must type var
    
    func originX() -> CGFloat {
        let version: String = UIDevice.currentDevice().systemVersion
        return version >= "7.0" ? 64.0 : 0.0
    }
        
    //
    // 重载需要使用关键字override
    override func viewDidLoad() {
        super.viewDidLoad() // 先调用父类的
     
        //
        // @name create button
        println(self.originX())
        let button = UIButton(frame: CGRectMake(30, self.originX() + 30, 100, 30))
        button.setTitle("button", forState:UIControlState.Normal)
        button.backgroundColor = UIColor.blackColor()
        button.setTitleColor(UIColor.whiteColor(), forState:.Normal)
        // add an action
        button.addTarget(self, action: "onButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)
        // @} end create button
        
        //
        // @name create label
        _label = UILabel(frame: CGRectMake(30, self.originX() + 70, 100, 60))
        _label!.text = "label"
        _label!.backgroundColor = UIColor.clearColor()
        _label!.textColor = UIColor.redColor()
        // use NSTextAlignment
        _label!.textAlignment = NSTextAlignment.Center
        self.view.addSubview(_label!)
        // @} end create label
        
        //
        // @name create UISlider
        let slider = UISlider(frame: CGRectMake(30, self.originX() + 130, 60, 30))
        slider.addTarget(self, action: "clickSlider:", forControlEvents: .ValueChanged)
        slider.minimumValue = 17.0
        slider.maximumValue = 36.0
        self.view.addSubview(slider)
        // @} end create UISlider
    }
    
    func onButtonClick(sender: UIButton!) {
       // UIAlertController.showAlert(self, title: "title", message: "message")
       // UIAlertController.showAlert(self, title: "title", message: "message", cancelButtonTitle: "cancel", okButtonTitle: "ok")
        UIAlertController.showAlert(self, title: "title", message: "message", cancelButtonTitle: "cancel", okButtonTitle: "ok", okHandler: {
            (UIAlertAction) in
            println("no nothing")
        })
    }
    
    func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
        println("clicked button at index: \(buttonIndex)")
    }
    
    func clickSlider(sender: UISlider!) {
        _label!.font = UIFont.systemFontOfSize(sender.value)
    }
}


你可能感兴趣的:(Swift UI学习之基础:UIButton,UILabel and UISlider)