swift delegate代理和block的使用
2016-11-18 17:04
309 查看
不多说直接上代码
声明// TestDelegateView.swift // blockAndDelegate // // Created by jarvis on 2016/11/18. // Copyright © 2016年 jarvis jiang. All rights reserved. import UIKit @objc protocol testDelegate { //代理声明 @objc optional func ClickEd(str: String) } class TestDelegateView: UIView { var delegate: testDelegate? // 声明block var buttonBlock :((_ str:String)->())? override init(frame:CGRect) { super.init(frame: frame) self.backgroundColor=UIColor.green let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20)) label.text="顶你个肺" label.textAlignment = .center self.addSubview(label) let button : UIButton = UIButton.init(frame: CGRect.init(x: 0, y: 25, width: 40, height: 40)) button.addTarget(self, action: #selector(buttonClicked(_:)) , for: .touchUpInside) button.backgroundColor=UIColor.red self.addSubview(button) } func buttonClicked(_ button:UIButton) -> Void{ print("buttonCLick") delegate?.ClickEd!(str: "代理点击") buttonBlock?("block点击") } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ }
调用
import UIKit import Alamofire class ViewController: UIViewController,testDelegate{ override func viewDidLoad() { super.viewDidLoad() initView() Alamofire.request("http://chenggua.com/api.php?r=verupdate/verupdate", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).validate().responseJSON { (response) in switch response.result{ case.success: print("success------\(response)") case.failure(let error): print(" error------|\(error)--"); } } // Do any additional setup after loading the view, typically from a nib. } func initView() -> Void { let tt = TestDelegateView.init(frame: CGRect.init(x: 0, y: 210, width: UIScreen.main.bounds.width, height: 100)) tt.delegate=self tt.buttonBlock = { (str:String) -> () in print("------blockClick---\(str)") } self.view.addSubview(tt) } // MARK: -----testDelegate func ClickEd(str: String) { print("-----clickString-------\(str)") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
相关文章推荐
- Swift基础--通知,代理和block的使用抉择以及Swift中的代理
- swift-delegate(代理)或者block传值
- Swift之delegate代理和Block闭包的反向传值
- 使用block造成的内存泄露和block和delegate的区别
- 代理的return怎么造成循环引用的,block的使用技巧
- 代理Delegate在写页面逻辑的使用
- Block使用详解,Block与代理相比的优点与缺点
- iOS编程——OC viewController 传值常用方法Delegate和Block的使用
- iPhone开发之Delegate(代理、委托)的理解和使用
- 【swift_4】swift之代理传值(delegate的用法)
- iOS开发Delegate,Notification,Block使用心得
- Swift - 委托(delegate)的介绍,及使用样例
- swift中代理的使用
- 常见的6种传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
- iOS学习11--多界面使用代理传值delegate
- 2015.7.30 第五堂(补)delegate 三部曲理解之一(不使用代理)
- iOS之代理delegate使用
- 在使用swift时碰到block 引用循环。特此记一笔。
- Swift入门教程18-代理Delegate
- iOS中block和代理的使用