// http://blog.csdn.net/woaifen3344/article/details/29840275
// 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,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,message: "message",cancelButtonTitle: "cancel",okButtonTitle: "ok")
UIAlertController.showAlert(self,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)
}
}
原文链接:https://www.f2er.com/swift/324609.html