学习任何语言都是从hello world开始的,哈哈哈
开始我的swift学习之旅
//这个好像就是类似于oc的懒加载 (个人观点--菜鸡观点) fileprivate var hellobtn: uibutton = { let hellobtn = uibutton(type:.custom) //初始化uibutton hellobtn.frame = cgrect(x: 100, y: 100, width: 205, height: 50) //设置frame hellobtn.backgroundcolor = uicolor.blue //设置背景颜色 hellobtn.settitle("欢迎", for: uicontrolstate.normal) //设置title (普通状态下) hellobtn.settitlecolor(uicolor.white, for: .normal) //设置title的颜色 (普通状态下) hellobtn.settitle("hello world", for: uicontrolstate.selected) //设置title (点击状态下) hellobtn.addtarget(self, action: #selector(hellobtnclick), for: .touchupinside) //添加点击事件 return hellobtn }()
至于我们需要实现什么效果,且等代码上完
初始化一个button ok了,就需要把它加载在view上显示出来
//这个方法相当于 oc里的 -(void)viewdidload; override func viewdidload() { super.viewdidload() //在view上添加一个按钮 self.view .addsubview(hellobtn) }
ok,还差一个点击事件的方法
extension viewcontroller{ //这个就是点击事件出发的方法 @objc fileprivate func hellobtnclick(sender :uibutton){ //改变状态 sender.isselected = !sender.isselected; }}
相关推荐:
学习标准——笔记_经验交流
php学习笔记之基础知识
以上就是swift学习笔记一 hello world的详细内容。