首先來(lái)講下閉包吧,其實(shí)閉包跟之前C中的block回調(diào)函數(shù)類似,但這里只研究了基礎(chǔ)的使用,我在下面的兩個(gè)VC中利用閉包做了通訊傳值,也算是比較常用的方法吧,回頭有時(shí)間我再研究下在項(xiàng)目中的其它應(yīng)用
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的玄武網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴! let sayHello = { println("nihao") } sayHello() //定義一個(gè)閉包函數(shù),與常規(guī)方法不同的是后面有個(gè)關(guān)鍵字in哦 let add = { (a: Int, b: Int) -> Int in return a + b } //調(diào)用的時(shí)候其實(shí)跟調(diào)用方法一樣哦 println(add(1, 2)) //下面就是一個(gè)簡(jiǎn)單的例子,來(lái)找出數(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)掉,直接改動(dòng)appdelegate里面的方法
UI這里面就沒(méi)有太多要講的,主要是多查查相關(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() //從語(yǔ)法上,我覺(jué)得跟O-C真的很不一樣,但是道理是通的,如果你的O-C語(yǔ)言還算熟練,我想上手swift語(yǔ)言也是很輕松的 let rootViewController = RootViewController() let navigationController = UINavigationController(rootViewController: rootViewController) navigationController.tabBarItem = UITabBarItem(title: "第一頁(yè)", p_w_picpath: nil, tag: 1) let secondViewController = SecondViewController() let secondNavigationController = UINavigationController(rootViewController: secondViewController) secondNavigationController.tabBarItem = UITabBarItem(title: "第二頁(yè)", p_w_picpath: nil, tag: 2) let array = [navigationController, secondNavigationController]; let tabBarController = UITabBarController() tabBarController.viewControllers = array self.window!.rootViewController = tabBarController return true }接下來(lái)我們創(chuàng)建兩個(gè)VC的類,Swift里面并沒(méi)有所謂的指定類創(chuàng)建,而是在swift文件里,我們可以創(chuàng)建好多好多的類,當(dāng)然了,為了更好的區(qū)分,我就單獨(dú)創(chuàng)建類吧
這樣我們?cè)趦蓚€(gè)類里面單獨(dú)創(chuàng)建一些基礎(chǔ)的控件,然后再寫(xiě)一個(gè)協(xié)議來(lái)運(yùn)用起來(lái)
主要還算來(lái)熟悉一下相關(guān)的語(yǔ)法
在下面的代碼中也用到了Protocol以及Closure,方便小伙伴們上手哦
class RootViewController: UIViewController, ViewChangeDelegate { var clickCount:Int = 0; var myLabel:UILabel? override func viewDidLoad() { super.viewDidLoad() self.title = "爐石傳說(shuō)" let nextItem = UIBarButtonItem(title: "下一頁(yè)", 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("點(diǎn)擊", forState: .Normal) myButton.addTarget(self, action: "clickMe:", forControlEvents: .TouchUpInside) self.view.addSubview(myButton) } func clickMe(sender:UIButton) { clickCount += 1; println("click\(clickCount)") myLabel!.text = "你猜我點(diǎn)了幾次呢,\(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 = "第二頁(yè)" 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("返回上一頁(yè)", 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("改變首頁(yè)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) }好啦,就先寫(xiě)這么多吧
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。