UILabel
用于顯示文本的控件,繼承于UIView,實(shí)現(xiàn)來NSCoding協(xié)議
class UILabel : UIView, NSCoding {...}
基本使用
let label = UILabel(frame:CGRect(origin: CGPointMake(10.0, 50.0), size: CGSizeMake(150,50)))
label.text = "This is a Label"
self.view.addSubview(label)
UIButton
按鈕控件繼承于UIControl ,實(shí)現(xiàn)NSCoding 協(xié)議混槐,UIControl繼承于UIView思喊,UIControl 實(shí)現(xiàn)的添加點(diǎn)擊時間函數(shù)
func addTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)
// remove the target/action for a set of events. pass in NULL for the action to remove all actions for that target
func removeTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)
基本使用
let btn = UIButton(frame: CGRect(origin: CGPointMake(20.0, 100.0), size: CGSizeMake(150,50)))
btn.setTitle("clickme", forState: UIControlState.Normal)
btn.backgroundColor = UIColor.blueColor()
btn.addTarget(self, action: "btnClickMe:", forControlEvents:UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func btnClickMe(sender:UIButton){
NSLog("btn clicked")
}
UIAlertView
彈出框控件授帕,使用時實(shí)現(xiàn) UIAlertViewDelegate,
基本使用
func btnClickMe(sender:UIButton){
NSLog("btn clicked")
var alertView = UIAlertView()
alertView.title = "Tips"
alertView.message = "密碼錯誤"
alertView.delegate = self
alertView.addButtonWithTitle("Cancel")
alertView.addButtonWithTitle("OK")
alertView.show()
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
NSLog("btn clicked \(buttonIndex)")//index 按照聲明的順序從0開始
}
UISwitch
開關(guān)按鈕
let switchBtn:UISwitch = UISwitch(frame: CGRect(origin: CGPointMake(100, 200), size: CGSizeMake(0, 0)))
//未選中顏色侥祭,只能顯示邊框
switchBtn.tintColor = UIColor.redColor()
//小按鈕
switchBtn.thumbTintColor = UIColor.greenColor()
//選中顏色
switchBtn.onTintColor = UIColor.blueColor()
self.view.addSubview(switchBtn)
棄用Storyboard
創(chuàng)建一個SingleView項(xiàng)目
刪除Main.storyboard文件
-
刪除info.plist里的main的引用
Main stroyboard file base name
補(bǔ)充AppDelegate 文件中的 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 方法
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
//定義一個視圖控制器
let one_vc = ViewController();
//創(chuàng)建導(dǎo)航控制器
let nvc=UINavigationController(rootViewController:one_vc);
//設(shè)置根視圖
self.window!.rootViewController=nvc;
self.window!.makeKeyAndVisible()
return true
}
UITableView
聲明一個tableView 平痰,Controller實(shí)現(xiàn) UITableViewDataSource, UITableViewDelegate 協(xié)議
var tableView : UITableView?
設(shè)置相關(guān)代碼
// 初始化tableView的數(shù)據(jù)
self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)
// 設(shè)置tableView的數(shù)據(jù)源
self.tableView!.dataSource=self
// 設(shè)置tableView的委托
self.tableView!.delegate = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(self.tableView!)
實(shí)現(xiàn)幾個回調(diào)函數(shù)
//列表的item數(shù)量
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
// item的內(nèi)容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
return UITableViewCell()
}
// 選中函數(shù)回調(diào)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){}