日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

swift_通知的使用

發布時間:2023/12/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 swift_通知的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

import UIKit

class ViewController: UIViewController {
//定義通知的名字,Notification 的“名字”不再是字符串類型,而是 Notification.Name 類型
let myNotification = Notification.Name(rawValue:"MyNotification")
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let nc = NotificationCenter.default
//發送通知
nc.post(name:myNotification,
object: nil,
userInfo:["message":"Hello there!", "date":Date()])
}
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
//添加通知,通知處理盡量
nc.addObserver(forName:myNotification, object:nil, queue:nil, using:catchNotification)

}
func catchNotification(notification:Notification) -> Void {
print("Catch notification")
//guard語句和if語句有點類似,都是根據其關鍵字之后的表達式的布爾值決定下一步執行什么。但與if語句不同的是,guard語句只會有一個代碼塊,不像if語句可以if else多個代碼塊。
//userInfo變量的作用域是在整個方法體內
//guard方法可以自動解包
guard let userInfo = notification.userInfo,
let message = userInfo["message"] ,
let date = userInfo["date"] else {
print("No userInfo found in notification")
return
}

//提醒框
let alert = UIAlertController(title: "Notification!",
message:"\(message) received at \(date)",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

轉載于:https://www.cnblogs.com/lcl15/p/7573484.html

總結

以上是生活随笔為你收集整理的swift_通知的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。