swift中闭包的传值用法

作者: 皮乐皮儿 | 来源:发表于2016-11-24 11:26 被阅读998次

这两天在练习swift的项目中遇到了一个难题,就是闭包的使用,在网络封装中或者是界面传值中,用OC中的block块传值很方便直观,在swift中却有些晕,经过搜索资料,终于解决了

主要是闭包的定义和使用,模拟一个场景,TopScrollView中点击按钮,利用闭包将所需的参数传递给控制器HomeViewController使用,控制器中调用闭包

1.在TopScrollView中定义闭包

class TopScrollView: UIScrollView {
    var config:ItemConfig!
    var currentIndex:Int = 0
    var lineView:UIView!
    var tapAnimation:Bool?
    //闭包的两种定义方法
    //1.
    typealias blockClosure = (Int,Bool) ->Void
    var didClickItemBlock:blockClosure?
    //2.
    //var itemDidClickBlock:((Int,Bool) ->Void)?
}
  • 闭包的定义就完成了,下面是触发闭包,用来传值
    @objc private func itemButtonClick(button:UIButton) {
    
        currentIndex = button.tag - 100
        if tapAnimation! {
        }else {
            changeItemColor(index: Float(currentIndex))
            changeLine(index: Float(currentIndex))
        }
        changeScrollOffset(index: Float(currentIndex))
//        topScrollViewDelegate!.topScrollViewDidClickItem(index: currentIndex, animation: tapAnimation!)
        if didClickItemBlock != nil {
            didClickItemBlock!(currentIndex,tapAnimation!)
        }
    }
  • 最后是在控制器中接收传递过来的值,做相应操作
    private func setupTopScrollView() {
    
        let config = ItemConfig()
        config.itemWidth = view.width / 4.0
        
        topScrollView = TopScrollView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 40))
        topScrollView.config = config
        let array = ["视频","直播","图集","小清新","程序设计","新闻","天气预报","段子","政治"]
        topScrollView.setupDataSource(array:array as NSArray)
        topScrollView.topScrollViewDelegate = self
        view.addSubview(topScrollView)
        topScrollView.didClickItemBlock = { (index,tapAnimation) in
        self.mainCollectionView.scrollRectToVisible(CGRect(x: CGFloat(index) * self.mainCollectionView.width, y: 0.0, width: self.mainCollectionView.width, height: self.mainCollectionView.height), animated: tapAnimation)  
        }
    }

整个闭包的使用就是这样,其实和block块很像,弄懂了也不是那么可怕,下面附一个简单网络请求中的闭包使用

class NetworkTool: NSObject {
    
    typealias successBlock = (Any?) ->Void
    typealias failureBlock = (NSError) ->Void
    func GetRequest(urlString:String,parameters:[String:Any]?,success:successBlock?,failure:failureBlock?) {
        
        let manager = AFHTTPSessionManager()
        
        manager.get(urlString, parameters: parameters, progress: {(progress) in
        
        }, success: {(task, response) in
        
            if success != nil {
            
                success!(response)
            }
            
        }, failure: {(task, error) in
        
            if failure != nil {
            
                failure!(error as NSError)
            }
        })
        
        
    }
    
}

相关文章

  • swift 闭包传值

    在oc 中,反向传值可以采用block块来实现,同样,在swift 中也有类似的闭包,下面就闭包传值进行简单的介绍...

  • swift中闭包的传值用法

    这两天在练习swift的项目中遇到了一个难题,就是闭包的使用,在网络封装中或者是界面传值中,用OC中的block块...

  • iOS swift 学习(二)

    Swift 闭包闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift 中...

  • Swift-传值坑

    Swift中block、代理、通知、单例传值 block传值 定义一个闭包实现block主要分三步: 定义一个闭包...

  • swift传值

    本文将介绍swift中的传值方式:属性传值、代理传值、闭包传值、通知传值本文将在两个VC之间进行传值:HomeVC...

  • Swift界面传值

    Swift中界面传值的方法 主要有三种 1.代理传值2.闭包传值(即OC中的Block) 属性传值 代理传值 F...

  • Swift:基础(十六)闭包

    Swift 闭包 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。 Swift...

  • Learning iOS D7 2017-10-30(传值方式

    Swift 4 四种传值方式 一:闭包传值(子vc传给父vc) 1.声明一个闭包(子vc) var closure...

  • Swift 闭包

    闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。 Swift 中的闭包与 C ...

  • Swift中的闭包

    一、简介 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值。Swift 中的闭包...

网友评论

  • SoaringHeart:topScrollView.didClickItemBlock = { (index,tapAnimation) in
    self.mainCollectionView.scrollRectToVisible(CGRect(x: CGFloat(index) * self.mainCollectionView.width, y: 0.0, width: self.mainCollectionView.width, height: self.mainCollectionView.height), animated: tapAnimation)
    }
    闭包不提示,有没有办法解决?

本文标题:swift中闭包的传值用法

本文链接:https://www.haomeiwen.com/subject/uqebpttx.html