热门搜索 :
考研考公
您的当前位置:首页正文

swift中的Delegate、Notification、Blo

来源:东饰资讯网

swift中的Delegate、Notification、block 的思路和实现过程和Object-C基本一致。只是语法上的差异而已。这里用这些知识点做了一个页面的交互,从ViewController页面进入secondCtl页面,在secondCtl页面通过delegate、通知、block传值到第一个页面。

  • ViewController页面有一个按钮,用于点击进入第二个页面,还有一个label,用于显示block、通知、delegate传回来的字符串。代码如下
//协议
protocol textDelegate:NSObjectProtocol {
    func dosomething(text:String)
}
import UIKit
//通知名
let postName = "PostNotificationname"
//类的内容
class ViewController: UIViewController,textDelegate {
    var ctr:secondCtl?    
    @IBOutlet weak var message: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(notific), name: NSNotification.Name(rawValue: postName), object: nil)
        ctr = secondCtl()
        ctr?.tdelegate = self
        ctr?.block = {(text:String) in
            self.message.text = text
        }
    }
    @IBAction func enter(_ sender: AnyObject) {
        self.present(ctr!, animated: true, completion: nil)
    }
    func notific() {
        message.text = "来自NotificationCenter的消息"
    }
    func dosomething(text: String) {
        message.text = text;}
}
  • secondCtl页面有三个按钮、分别触发通知、dedegate、block的事件。代码如下。
import UIKit
//定义一个block
typealias blocktype = (_ text:String)->Void
class secondCtl: UIViewController {
    var block:blocktype?
    weak var tdelegate:textDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @IBAction func delegateTransport(_ sender: AnyObject) {
                self.tdelegate?.dosomething(text: "我来自代理")
        self.close(nil)
    }
    @IBAction func notifictionTransport(_ sender: AnyObject) {
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: postName), object: self, userInfo: nil)
        self.close(nil)
    }
    @IBAction func blockTransport(_ sender: AnyObject) {
        if block != nil{       
            block!("block传的值")
            self.close(nil)
        }
    }
    @IBAction func close(_ sender: AnyObject?) {
        self.dismiss(animated: true, completion: nil)   }
}
secondCtl页面

这样看起来比较繁琐,下面一条一条的拎出来看。

Notification

1、定义一个全局变量,作为通知名称。swift的全局变量和OC的有点不一样,其他文件若是想要访问该全局变量,OC需要倒入定义全局变量的头,而swift不需要。

let postName = "PostNotificationname"

2、注册通知监听

 NotificationCenter.default.addObserver(self, selector: #selector(notific), name: NSNotification.Name(rawValue: postName), object: nil)

3、监听执行的事件

func notific() {
        message.text = "来自NotificationCenter的消息"; 
}

Delegate

delegate通常用于在特定的时间点改变事件的传递链的使用上,常做回调使用。

1、声明协议,协议只有一个方法

protocol textDelegate:NSObjectProtocol {
    func dosomething(text:String)
}

2、委托需要去遵守这个协议并实现这个方法

class ViewController: UIViewController,textDelegate {
 override func viewDidLoad() {
        super.viewDidLoad()
        ctr = secondCtl()
      //成为代理       
      ctr?.tdelegate = self
    }
             ...
    func dosomething(text: String) {
        message.text = text;
    }
              ...
}

3、实现回调,在secondCtl中回调

 self.tdelegate?.dosomething(text: "我来自代理")

Block

1、在secondCtl定义一个block类型

typealias blocktype = (_ text:String)->Void

2、secondCtl,声明一个属性为blocktype类型

    var block:blocktype?

3、Block回调

    @IBAction func blockTransport(_ sender: AnyObject) {
        
        if block != nil{
            block!("block传的值")
            self.close(nil)
          }
    }

4、在ViewController中处理block的回调事件

        ctr = secondCtl()
        ctr?.tdelegate = self
        ctr?.block = {(text:String) in
        
            self.message.text = text
        }

总结

Top