首先來講下閉包吧,其實閉包跟之前C中的block回調(diào)函數(shù)類似,但這里只研究了基礎(chǔ)的使用,我在下面的兩個VC中利用閉包做了通訊傳值,也算是比較常用的方法吧,回頭有時間我再研究下在項目中的其它應(yīng)用
創(chuàng)新互聯(lián)公司云計算的互聯(lián)網(wǎng)服務(wù)提供商,擁有超過13年的服務(wù)器租用、成都服務(wù)器托管、云服務(wù)器、網(wǎng)頁空間、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗,已先后獲得國家工業(yè)和信息化部頒發(fā)的互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務(wù)許可證。專業(yè)提供云主機(jī)、網(wǎng)頁空間、空間域名、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
let sayHello = { println("nihao") } sayHello() //定義一個閉包函數(shù),與常規(guī)方法不同的是后面有個關(guān)鍵字in哦 let add = { (a: Int, b: Int) -> Int in return a + b } //調(diào)用的時候其實跟調(diào)用方法一樣哦 println(add(1, 2)) //下面就是一個簡單的例子,來找出數(shù)組中大于等于value的值,如果有,返回Yes var array = [20, 9, 100, 34, 89, 39] func hasClosureMatch(array: [Int], value: Int, closureValue: (num:Int, value:Int)-> Bool)-> Bool { for item in array { if (closureValue(num: item, value: value)) { return true } } return false } //Closure 閉包 var v1 = hasClosureMatch(array, 40) { (num, value) -> Bool in return num >= value } println(v1)
然后是UI基礎(chǔ)的代碼,可以直接創(chuàng)建單一控制器的工程,主要是為了熟悉一下代碼
這里我們可以先把storyboard關(guān)掉,直接改動appdelegate里面的方法
UI這里面就沒有太多要講的,主要是多查查相關(guān)的API,然后慢慢積累咯
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.backgroundColor = UIColor.whiteColor() self.window!.makeKeyAndVisible() //從語法上,我覺得跟O-C真的很不一樣,但是道理是通的,如果你的O-C語言還算熟練,我想上手swift語言也是很輕松的 let rootViewController = RootViewController() let navigationController = UINavigationController(rootViewController: rootViewController) navigationController.tabBarItem = UITabBarItem(title: "第一頁", p_w_picpath: nil, tag: 1) let secondViewController = SecondViewController() let secondNavigationController = UINavigationController(rootViewController: secondViewController) secondNavigationController.tabBarItem = UITabBarItem(title: "第二頁", p_w_picpath: nil, tag: 2) let array = [navigationController, secondNavigationController]; let tabBarController = UITabBarController() tabBarController.viewControllers = array self.window!.rootViewController = tabBarController return true }
接下來我們創(chuàng)建兩個VC的類,Swift里面并沒有所謂的指定類創(chuàng)建,而是在swift文件里,我們可以創(chuàng)建好多好多的類,當(dāng)然了,為了更好的區(qū)分,我就單獨創(chuàng)建類吧
這樣我們在兩個類里面單獨創(chuàng)建一些基礎(chǔ)的控件,然后再寫一個協(xié)議來運用起來
主要還算來熟悉一下相關(guān)的語法
在下面的代碼中也用到了Protocol以及Closure,方便小伙伴們上手哦
class RootViewController: UIViewController, ViewChangeDelegate { var clickCount:Int = 0; var myLabel:UILabel? override func viewDidLoad() { super.viewDidLoad() self.title = "爐石傳說" let nextItem = UIBarButtonItem(title: "下一頁", style: .Plain, target: self, action: "nextPage:") self.navigationItem.rightBarButtonItem = nextItem myLabel = UILabel(frame: CGRect(x: 0, y: 100, width: 320, height: 44)) myLabel!.text = "小華,你好啊" myLabel!.backgroundColor = UIColor.redColor() self.view.addSubview(myLabel!) var myButton = UIButton(frame: CGRect(x: 100, y: 200, width: 100, height: 44)) myButton.backgroundColor = UIColor.blueColor() myButton.setTitle("點擊", forState: .Normal) myButton.addTarget(self, action: "clickMe:", forControlEvents: .TouchUpInside) self.view.addSubview(myButton) } func clickMe(sender:UIButton) { clickCount += 1; println("click\(clickCount)") myLabel!.text = "你猜我點了幾次呢,\(clickCount)" } func nextPage(sender:UIButton) { let secondViewController = SecondViewController() secondViewController.viewChangeDelegate = self secondViewController.changeTextForClosure("1", num: 1) { (value, num) -> Void in myLabel?.text = value } self.navigationController?.pushViewController(secondViewController, animated: true) } func changeTitleToString(controller:UIViewController, value:String) { myLabel!.text = value }
import Foundation import UIKit class SecondViewController: UIViewController { var viewChangeDelegate:ViewChangeDelegate? var closure = { (value:String, num:Int) -> Void in } override func viewDidLoad() { super.viewDidLoad() self.title = "第二頁" self.view.backgroundColor = UIColor.grayColor() var button = UIButton.buttonWithType(.System) as! UIButton button.frame = CGRect(x: 100, y: 100, width: 100, height: 40) button.setTitle("返回上一頁", forState: .Normal) button.addTarget(self, action: "back:", forControlEvents: .TouchUpInside) self.view.addSubview(button) var buttonChange = UIButton.buttonWithType(.System) as! UIButton buttonChange.frame = CGRect(x: 100, y: 200, width: 100, height: 40) buttonChange.setTitle("改變首頁label值", forState: .Normal) buttonChange.addTarget(self, action: "change:", forControlEvents: .TouchUpInside) self.view.addSubview(buttonChange) } func changeTextForClosure(value:String, num:Int, closureValue:(value:String, num:Int) -> Void) { self.closure = closureValue } func change(sender:UIButton) { if ((viewChangeDelegate) != nil) { viewChangeDelegate?.changeTitleToString(self, value: "我變變變") } self.closure("你好", 1) } func back(sender:UIButton) { self.navigationController?.popToRootViewControllerAnimated(true) } } protocol ViewChangeDelegate : NSObjectProtocol { func changeTitleToString(controller:UIViewController, value:String) }
好啦,就先寫這么多吧