您好,登錄后才能下訂單哦!
首先來講下閉包吧,其實閉包跟之前C中的block回調函數類似,但這里只研究了基礎的使用,我在下面的兩個VC中利用閉包做了通訊傳值,也算是比較常用的方法吧,回頭有時間我再研究下在項目中的其它應用
let sayHello = { println("nihao") } sayHello() //定義一個閉包函數,與常規方法不同的是后面有個關鍵字in哦 let add = { (a: Int, b: Int) -> Int in return a + b } //調用的時候其實跟調用方法一樣哦 println(add(1, 2)) //下面就是一個簡單的例子,來找出數組中大于等于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基礎的代碼,可以直接創建單一控制器的工程,主要是為了熟悉一下代碼
這里我們可以先把storyboard關掉,直接改動appdelegate里面的方法
UI這里面就沒有太多要講的,主要是多查查相關的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 }
接下來我們創建兩個VC的類,Swift里面并沒有所謂的指定類創建,而是在swift文件里,我們可以創建好多好多的類,當然了,為了更好的區分,我就單獨創建類吧
這樣我們在兩個類里面單獨創建一些基礎的控件,然后再寫一個協議來運用起來
主要還算來熟悉一下相關的語法
在下面的代碼中也用到了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) }
好啦,就先寫這么多吧
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。