swift篇第四期:闭包、UI基础、Protocol
admin
2023-02-10 12:40:12
0


首先来讲下闭包吧,其实闭包跟之前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)
}



好啦,就先写这么多吧


相关内容

热门资讯

多地官宣结婚生娃发钱,最高20... 近日,广东佛山顺德陈村镇大都村通过股东会议表决,将生育结婚奖励政策纳入《佛山市顺德区陈村镇大都村股份...
蒋万安、卢秀燕、谢国梁开记者会... 海峡导报综合报道 民进党当局核准台湾“中联”19批油品即日起重新上架,台“食药署”23日首度公布共计...
西门子洗衣机不工作了什么原因 西门子洗衣机不工作了什么原因1、可能是使用时间太久了,导致洗衣机内部零件出现了问题。2、可能是长时间...
不工作时西门子洗衣机一直响 1、洗衣机的电源电压过低导致西门子洗衣机一直响。2、洗衣机的电容器故障。3、洗衣机的转子卡住导致西门...
博世洗衣机泡沫过多机器不转了 可能是系统部分堵塞。要立即修复单次洗涤,在分配器抽屉上加一顶织物调节剂即可,最好在机器下次用水冲洗时...
14名残疾人用AI做了部爆款短... 执笔 王波日前,宁波众闻道软件科技有限公司负责人龙杰收到了6月份AI短剧收益报告。在公司当月上线的4...
小米洗衣机不运转了怎么修 小米洗衣机不运转,可能是由于多种原因引起的,可能是由于电源问题、电机损坏、控制面板故障等原因。下面是...
杂牌洗衣机不转了怎么办 如果杂牌洗衣机出现不转的故障,可能会造成很多麻烦。无法使用洗衣机会影响我们的日常生活,但是针对这个问...
晒出317万年终奖后,腾讯一员... 近日,有网友晒出疑似腾讯内部发布的通报:WXG某项目组负责人叶某在职期间,因私自泄露公司敏感信息,被...
公安部:国际打击电信网络诈骗联... 今天(24日)上午,国务院新闻办公室举行“开局起步‘十五五’”系列主题新闻发布会,公安部相关负责人介...