前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift 2.2 协议和代理

Swift 2.2 协议和代理

作者头像
Mr.RisingSun
发布2018-01-09 17:03:50
6070
发布2018-01-09 17:03:50
举报
文章被收录于专栏:移动端开发

一:代理 

    两个类之间的传值,类A调用类B的方法,类B在执行过程中遇到问题通知类A,这时候我们需要用到代理(Delegate)。

    比如:控制器(Controller)与控制器(Controller)之间的传值,从C1跳转到C2,再从C2返回到C1时需要通知C1更新UI或者是做其它的事情,这时候我们就用

到了代理(Delegate)传值。

二:协议

    上面说的两个界面,或者类之间的值传递,你就把协议当成他们之间的合同,就理解了。

    下面看看Swift代码怎么写协议,为了方便阅读,我这里把整个Swift文件代码插入了,不是只写了一个方法。这样我自己觉得阅读性更强一点!

代码语言:javascript
复制
import UIKit

// 协议的创建
protocol youname{    
    func younameis(name:NSString) -> Void
}
class ProfileViewController: UIViewController{
    
   // 声明一个协议类型的代理变量
    var delegate:youname!

    // 这个方法写在这里只是为了在后面的界面设置了代理之后,去调用这个方法。
    func delegatetest() -> Void {
        
        self.delegate.younameis("caoxiaocaoxiangni")
    }
   // 这里的思路其实和我们OC写的思路是一样的!
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.whiteColor()
    }
  
    override func didReceiveMemoryWarning() {
        
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

     我们来看看第二个界面里面是怎么写的,上面一个是 ProfileViewController 控制器,push 到下一个控制器,MEViewController 中。

代码语言:javascript
复制
import UIKit

class MEViewController: UIViewController,youname {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        // 创建了变量,设置代理,遵守协议,调用方法。
        let you : ProfileViewController = ProfileViewController()
        you.delegate=self
        you.delegatetest()
        
    }
    // 上个控制器里面  delegatetest 这个方法中,我们又设置了让它的代理(其实就是MEViewController)调用 younameis 方法,前面界面的参数 caoxiaocao 也就传了过来!

    func younameis(name: NSString) {
     
           print(name) // caoxiaocao
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

这样子,我也就完整的吧上个界面的值,传到了这个界面中来了。整个思路和OC的差不多。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档