美文网首页
swift协议和代理

swift协议和代理

作者: 不简单的风度 | 来源:发表于2016-06-23 16:27 被阅读203次

本文只是用一个小Demo来说明swift中协议和代理的用法
首先自定义了一个view,并在view中实现了定义了协议方法,然后在controller中使用自定义view并实现协议和代理
自定义view代码如下:

import UIKit

@objc protocol CustomViewDelegae {
    func clickAtIndex(index:NSInteger)
}

class CustomView: UIView {
    
    weak var delegate: CustomViewDelegae?
    var button1:UIButton?
    var button2:UIButton?
    
    
    init(delegate: CustomViewDelegae) {
        super.init(frame: CGRectZero)
        self.delegate = delegate
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        self.button1 = UIButton.init(type: .Custom)
        self.button1?.backgroundColor = UIColor.redColor()
        self.button1?.frame = CGRectMake(0, 65, 60, 30)
        self.button1?.tag = 1
        self.button1?.addTarget(self, action: #selector(buttonClick), forControlEvents: .TouchUpInside)
        self.addSubview(self.button1!)
        
        
        self.button2 = UIButton.init(type: .Custom)
        self.button2?.backgroundColor = UIColor.blueColor()
        self.button2?.frame = CGRectMake(0, 100, 60, 30)
        self.button2?.tag = 2
        self.button2?.addTarget(self, action: #selector(buttonClick), forControlEvents: .TouchUpInside)
        self.addSubview(self.button2!)
        
        
    }
    
    func buttonClick(sender:UIButton){
        
        clickAtIndex(sender.tag)
    }
    

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}

extension CustomView {
    
    func clickAtIndex(index:NSInteger) {
        
        delegate?.clickAtIndex(index)
        
        print("you have clicked:",index)
        
    }
}

然后是controller中的代码:

import UIKit

class ViewController: UIViewController,CustomViewDelegae {
    
    var name:String?
    var cv:CustomView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        
        configViews()
        
    }
    
    func configViews() {
        
        self.title = "首页"
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        setupData()
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController {
    
    func setupData(){
        self.cv = CustomView.init(delegate: self)
        self.cv!.frame = CGRectMake(10, 10, 300, 300)
        self.cv!.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(self.cv!)
        
        
    }
    
    func clickAtIndex(index: NSInteger) {
        
        if index == 1 {
            let vc = SecondViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        else
        {
            print("click two")
        }
        
    }
}

以上就是swift中代理的写法,欢迎指正交流。

相关文章

  • swift协议和代理

    本文只是用一个小Demo来说明swift中协议和代理的用法首先自定义了一个view,并在view中实现了定义了协议...

  • iOS开发基础:协议、代理、block

    一、协议和代理 首先如果要学习协议和代理,要先了解一下什么是协议和代理 协议(protocol) 协议就是定义一个...

  • swift中的 扩展,协议和代理

    extensions 用于扩展现有的数据结构。你可以添加方法/属性 到 一个 类/结构体/枚举 (即便你没有源码)...

  • 协议和代理

    protocol-协议,就是使用了这个协议后就要按照这个协议来办事,协议要求实现的方法就一定要实现。delegat...

  • 协议和代理

    协议 //定义:一组方法的列表//特点:只有定义,没有实现,实现在引用(遵守)了该协议的类的.m文件中 格式@pr...

  • 协议和代理

    协议: Objective-C 里面把方法的声明写在 protocol 中,并给某个类添加 id 类型的 dele...

  • 在UIView上添加触摸事件

    方法1.协议和代理在UIView中设置协议和代理属性 让UIViewController遵守协议并实现协议中的中的...

  • 系列:iOS开发-协议和代理

    系列:iOS开发-协议和代理 既然说到了协议和代理,那么就有必要解释下意思.协议:协议是一个方法签名的列表,在这个...

  • Swift及SwiftUI学习笔记

    持续更新中...... swift官方文档 swift官方文档(英文) 协议 swift主要基于协议编程的,所以协...

  • swift开发的小坑

    swift 几个比较好的UI库 swift UI库 1.tableView的代理方法 在swift中代理变得更加重...

网友评论

      本文标题:swift协议和代理

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