swift篇第四期:闭包、UI基础、Protocol

swift篇第四期:闭包、UI基础、Protocol,第1张

概述首先来讲下闭包吧,其实闭包跟之前C中的block回调函数类似,但这里只研究了基础的使用,我在下面的两个VC中利用闭包做了通讯传值,也算是比较常用的方法吧,回头有时间我再研究下在项目中的其它应用  let sayHello = {    println("nihao")}sayHello()//定义一个闭包函数,与常规方法不同的是后面有个关键字in哦let add = { (a: I


首先来讲下闭包吧,其实闭包跟之前C中的block回调函数类似,但这里只研究了基础的使用,我在下面的两个VC中利用闭包做了通讯传值,也算是比较常用的方法吧,回头有时间我再研究下在项目中的其它应用

letsayHello={println("nihao")}sayHello()//定义一个闭包函数,与常规方法不同的是后面有个关键字in哦letadd={(a:Int,b:Int)->Intinreturna+b}//调用的时候其实跟调用方法一样哦println(add(1,2))//下面就是一个简单的例子,来找出数组中大于等于value的值,如果有,返回Yesvararray=[20,9,100,34,89,39]funchasClosureMatch(array:[Int],value:Int,closureValue:(num:Int,value:Int)->Bool)->Bool{foriteminarray{if(closureValue(num:item,value:value)){returntrue}}returnfalse}//Closure闭包varv1=hasClosureMatch(array,40){(num,value)->Boolinreturnnum>=value}println(v1)


然后是UI基础的代码,可以直接创建单一控制器的工程,主要是为了熟悉一下代码

这里我们可以先把storyboard关掉,直接改动appdelegate里面的方法

UI这里面就没有太多要讲的,主要是多查查相关的API,然后慢慢积累咯

funcapplication(application:UIApplication,dIDFinishLaunchingWithOptionslaunchOptions:[NSObject:AnyObject]?)->Bool{//OverrIDepointforcustomizationafterapplicationlaunch.self.window=UIWindow(frame:UIScreen.mainScreen().bounds)self.window!.backgroundcolor=UIcolor.whitecolor()self.window!.makeKeyAndVisible()//从语法上,我觉得跟O-C真的很不一样,但是道理是通的,如果你的O-C语言还算熟练,我想上手swift语言也是很轻松的letrootVIEwController=RootVIEwController()letnavigationController=UINavigationController(rootVIEwController:rootVIEwController)navigationController.tabbarItem=UITabbarItem(Title:"第一页",image:nil,tag:1)letsecondVIEwController=SecondVIEwController()letsecondNavigationController=UINavigationController(rootVIEwController:secondVIEwController)secondNavigationController.tabbarItem=UITabbarItem(Title:"第二页",tag:2)letarray=[navigationController,secondNavigationController];lettabbarController=UITabbarController()tabbarController.vIEwControllers=arrayself.window!.rootVIEwController=tabbarControllerreturntrue}


接下来我们创建两个VC的类,Swift里面并没有所谓的指定类创建,而是在swift文件里,我们可以创建好多好多的类,当然了,为了更好的区分,我就单独创建类吧

这样我们在两个类里面单独创建一些基础的控件,然后再写一个协议来运用起来

主要还算来熟悉一下相关的语法

在下面的代码中也用到了Protocol以及Closure,方便小伙伴们上手哦

classRootVIEwController:UIVIEwController,VIEwChangeDelegate{varclickCount:Int=0;varmyLabel:UILabel?overrIDefuncvIEwDIDLoad(){super.vIEwDIDLoad()self.title="炉石传说"letnextItem=UIbarbuttonItem(Title:"下一页",style:.Plain,target:self,action:"nextPage:")self.navigationItem.rightbarbuttonItem=nextItemmyLabel=UILabel(frame:CGRect(x:0,y:100,wIDth:320,height:44))myLabel!.text="小华,你好啊"myLabel!.backgroundcolor=UIcolor.redcolor()self.vIEw.addSubvIEw(myLabel!)varmybutton=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)}funcclickMe(sender:UIbutton){clickCount+=1;println("click\(clickCount)")myLabel!.text="你猜我点了几次呢,\(clickCount)"}funcnextPage(sender:UIbutton){letsecondVIEwController=SecondVIEwController()secondVIEwController.vIEwChangeDelegate=selfsecondVIEwController.changeTextForClosure("1",num:1){(value,num)->VoIDinmyLabel?.text=value}self.navigationController?.pushVIEwController(secondVIEwController,animated:true)}funcchangeTitleToString(controller:UIVIEwController,value:String){myLabel!.text=value}
importFoundationimportUIKitclassSecondVIEwController:UIVIEwController{varvIEwChangeDelegate:VIEwChangeDelegate?varclosure={(value:String,num:Int)->VoIDin}overrIDefuncvIEwDIDLoad(){super.vIEwDIDLoad()self.title="第二页"self.vIEw.backgroundcolor=UIcolor.graycolor()varbutton=UIbutton.buttonWithType(.System)as!UIbuttonbutton.frame=CGRect(x:100,height:40)button.setTitle("返回上一页",forState:.normal)button.addTarget(self,action:"back:",forControlEvents:.touchUpInsIDe)self.vIEw.addSubvIEw(button)varbuttonChange=UIbutton.buttonWithType(.System)as!UIbuttonbuttonChange.frame=CGRect(x:100,height:40)buttonChange.setTitle("改变首页label值",forState:.normal)buttonChange.addTarget(self,action:"change:",forControlEvents:.touchUpInsIDe)self.vIEw.addSubvIEw(buttonChange)}funcchangeTextForClosure(value:String,num:Int,closureValue:(value:String,num:Int)->VoID){self.closure=closureValue}funcchange(sender:UIbutton){if((vIEwChangeDelegate)!=nil){vIEwChangeDelegate?.changeTitleToString(self,value:"我变变变")}self.closure("你好",1)}funcback(sender:UIbutton){self.navigationController?.popToRootVIEwControllerAnimated(true)}}protocolVIEwChangeDelegate:NSObjectProtocol{funcchangeTitleToString(controller:UIVIEwController,value:String)}



好啦,就先写这么多吧

总结

以上是内存溢出为你收集整理的swift篇第四期:闭包、UI基础、Protocol全部内容,希望文章能够帮你解决swift篇第四期:闭包、UI基础、Protocol所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存