Notification的简单使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的简单使用的主要内容,如果未能解决你的问题,请参考以下文章
delegate, Notification,KVO优缺点?
Firebase Cloud 消息传递和 Azure Notification Hubs 设备注册相互实现
ZPush--基于netty4实现的苹果通知推送服务(APNs)Javaclient