First of all, the closure of the packet, in fact, the closures and the previous block callback function in c similar, but here only to study the use of the basic, I in the following two VC use closure to do the communication value, is also a more commonly used method bar, I have time to look back on the project in other applications
let sayhello = { println ("Nihao")}sayhello ()//define a closure function, Unlike conventional methods, there is a keyword in the let add = { (a: int, b: int) -> int in The return a + b}//call is actually the same as invoking the method Oh println (Add (1, 2))//The following is a simple example, To find a value greater than or equal to value in the array, and if so, return Yesvar array = [20, 9, 100, 34, 89, 39]func hasclosurematch (array: [int], value: int, closurevalue: (Num:Int, value:I NT)-> bool)-> bool { for item in array { if (Closurevalue (num: item, value: value)) { return true } } return false}//closure Closed Bag Var v1 = hasClosurematch (array, 40) { (num, value) -> Bool in return num >= value}println (v1)
Then there is the UI-based code that can directly create a single controller project, primarily to familiarize yourself with the code
Here we can first turn off the storyboard, directly change the method inside the Appdelegate
UI here there is not much to talk about, mainly to check the relevant API, and then slowly accumulate
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 () //from grammar, I think with o-c really very different, but the truth is through, if your o-c language is still skilled, I want to get started Swift language is also very easy let rootviewcontroller = rootviewcontroller () let navigationcontroller = uinavigationcoNtroller (Rootviewcontroller: rootviewcontroller) Navigationcontroller.tabbaritem = uitabbaritem (title: "First page", image: nil, tag:  1) let secondviewcontroller = secondviewcontroller () Let secondnavigationcontroller = uinavigationcontroller (rootviewcontroller: Secondviewcontroller) secondnavigationcontroller.tabbaritem = uitabbaritem (title: "second page", image: nil, tag: 2) let array = [ Navigationcontroller, secondnavigationcontroller]; let tabbarcontroller = uitabbarcontroller () tabbarcontroller.viewcontrollers = array self.window!. rootviewcontroller = tabbarcontroller return true }
Next we create two VC classes, Swift does not have the so-called designated class creation, but in the swift file, we can create a lot of many classes, of course, for a better distinction, I would like to create a separate class bar
So we create some basic controls in two classes, and then write a protocol to use it.
It's mostly about getting to know the grammar.
In the following code also used in protocol and closure, easy for small partners to get started OH
class RootViewController: UIViewController, ViewChangeDelegate { var clickCount:Int = 0; var myLabel:UILabel? override func viewdidload () { super.viewdidload () self.title = "Furnace stone legend" let nextitem = uibarbuttonitem (title: "Next page", style: . plain, target: self, action: "NextPage:") self.navigationitem.rightbarbuttonitem = nextitem mylabel = uilabel (Frame: CGRect (x: 0, y: 100, width: 320, height: 44)) mylabel!. text = "Xiao Hua, hello" 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 ("Click", forState: . Normal) mybutton.addtarget (self, action: "ClickMe:", forcontrolevents: . Touchupinside) self.view.addsubview (MyButton) } &Nbsp; func clickme (Sender:uibutton) { clickcount += 1; println (" Click\ (Clickcount) ") mylabel!. text = "You guess I ordered a few times, \ (clickcount)" } func nextpage (Sender:uibutton) { let Secondviewcontroller = secondviewcontroller () secondviewcontroller.viewchangedelegate = self Secondviewcontroller.changetextforclosure ("1", num: 1) { (value, num) -> void in mylabel?. Text = value } &nbSp; self.navigationcontroller?. Pushviewcontroller (secondviewcontroller, animated: true) } func changetitletostring (controller:uiviewcontroller, value:string) { mylabel!. text = value }
import foundationimport uikitclass secondviewcontroller: uiviewcontroller { var viewChangeDelegate:ViewChangeDelegate? var closure = { (Value:string, num:int) -> Void in } override func viewdidload () { super.viewdidload () self.title = "second page" 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 ("Return to Previous Page", 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 ("Change Home label value", 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: "I Change to change") } self.closure ("Hello", 1) } func back (Sender:uibutton) { self.navigationcontroller?. Poptorootviewcontrolleranimated (True) }}protocol ViewChangeDelegate : Nsobjectprotocol { func changetitletostring (Controller:UIViewController, value:string)}
All right, let's just write this a lot.
This article is from the "Neusoft iOS Alumni Group Technology Blog" blog, please be sure to keep this source http://neusoftios.blog.51cto.com/9977509/1674151
Swift Chapter Fourth: Closures, UI basics, Protocol