首页 > 代码库 > Notification的简单使用

Notification的简单使用

//

//  ViewController.swift

//  xcode8Beta测试

//

//  Created by zhangxu on 16/8/31.

//  Copyright © 2016年 zhangxu. All rights reserved.

//

 

import UIKit

 

class ViewController: UIViewController {

    

    let SendNotification = NSNotification.Name.init(rawValue: "SendNotification");

 

    override func viewDidLoad() {

        super.viewDidLoad()

        

        // 添加通知

        NotificationCenter.default.addObserver(self, selector: #selector(receivedNotification(notification:)), name: SendNotification, object: nil);

        

        // Do any additional setup after loading the view, typically from a nib.

    }

    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        

        // 发送通知

        NotificationCenter.default.post(name: SendNotification, object: nil, userInfo: ["SendNotification": "发送通知"]);

        

    }

    

    // MARK: - 收到通知

    func receivedNotification(notification: NSNotification) -> Void {

        

        let string = notification.userInfo!["SendNotification"];

        print(string ?? "");

   }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

 

}

 

Notification的简单使用