Swift-通知中心(NotificationCenter)的使用

NotificationCenter是Swift中一个调度消息通知的类,采用单例模式设计,实现传值、回调等作用.

通知的作用还是挺强大的,对于两个不相关的控制器之间,要进行信息的传递,使用通知是个不错的选择.


1、添加通知
     /// 通知名
     let notificationName = "XMNotification"
     /// 自定义通知
     NotificationCenter.default.addObserver(self, selector: #selector(notificationAction), name: NSNotification.Name(rawValue: notificationName), object: nil)

2、设置监听方法
    /// 接受到通知后的方法回调
    @objc private func notificationAction(noti: Notification) {
        /// 获取键盘的位置/高度/时间间隔...
        print(noti)
    }

3、在通知用完后及时销毁
    /// 析构函数.类似于OC的 dealloc
    deinit {
        /// 移除通知
        NotificationCenter.default.removeObserver(self)
    }

4、发送通知
    /// 发送简单数据
    NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "XMNotification"), object: "Hello 2017")

    /// 发送额外数据
    let info = ["name":"Eric","age":21] as [String : Any]
    NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "XMNotification"), object: "GoodBye 2016", userInfo: info)

通知在系统中的运用,监听键盘的变动

     /// 通知中心监听键盘的变化
     #selector(notificationAction), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

有关键盘的其他通知名称

     public static let UIKeyboardWillShow: NSNotification.Name
     /// 键盘显示完毕
     public static let UIKeyboardDidShow: NSNotification.Name
     /// 键盘将要隐藏
     public static let UIKeyboardWillHide: NSNotification.Name
     /// 键盘隐藏完毕
     public static let UIKeyboardDidHide: NSNotification.Name
     /// 键盘将要改变自身的frame
     public static let UIKeyboardWillChangeFrame: NSNotification.Name
     /// 键盘frame改变完成
     public static let UIKeyboardDidChangeFrame: NSNotification.Name
SwiftUI中,实现通知中心与消息列表的互动通常涉及到`NotificationCenter`和数据绑定功能。首先,你需要做的是请求用户的授权以便接收通知。这可以通过`UNUserNotificationCenter.requestAuthorization()`完成。 当你接收到通知时,你可以创建一个新的`Notification`对象,并在`View`中处理它。例如: ```swift struct MessageList: View { @StateObject private var notificationManager = NotificationCenterProxy() var body: some View { List { ForEach(notificationManager.receivedNotifications) { notification in NotificationItem(notification: notification) .onTapGesture { // 处理点击事件,如跳转到详细信息页面或更新消息列表 handleNotificationClick(notification) } } // 其他消息列表内容... } .onChange(of: notificationManager.receivedNotifications) { _ in // 当新的通知到达时,刷新消息列表 self.messageList.reloadData() } } func handleNotificationClick(_ notification: UNNotification) { // 根据通知的具体内容展示对应的消息详情 } } ``` `NotificationCenterProxy`是一个辅助类,用于简化对通知中心的操作,例如监听通知: ```swift class NotificationCenterProxy: ObservableObject { @Published var receivedNotifications = [UNNotification]() init() { UNUserNotificationCenter.current()..addObserver(self, selector: #selector(handleNotification(_:)), name: nil, object: nil) } deinit { UNUserNotificationCenter.current().removeObserver(self) } @objc func handleNotification(_ notification: UNNotification) { receivedNotifications.append(notification) } } ``` 在这个例子中,当用户点击通知时,会触发`handleNotificationClick`函数,可以在此处进一步处理通知的内容,并可能更新消息列表显示相应的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值