{"id":208,"date":"2022-08-31T23:14:10","date_gmt":"2022-08-31T15:14:10","guid":{"rendered":"https:\/\/www.jinhaihan.top\/WordPress\/?p=208"},"modified":"2022-08-31T23:14:13","modified_gmt":"2022-08-31T15:14:13","slug":"swiftui-%e5%ba%94%e7%94%a8%e5%86%85%e4%ba%8b%e4%bb%b6%e9%80%9a%e7%9f%a5-notificationcenter","status":"publish","type":"post","link":"https:\/\/www.jinhaihan.top\/WordPress\/swiftui-%e5%ba%94%e7%94%a8%e5%86%85%e4%ba%8b%e4%bb%b6%e9%80%9a%e7%9f%a5-notificationcenter\/","title":{"rendered":"SwiftUI-\u5e94\u7528\u5185\u4e8b\u4ef6\u901a\u77e5 &#8211; NotificationCenter"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><h1>\u5f00\u53d1\u73af\u5883<\/h1>\n<p>XCode 14 Beta\nMacos 13\nMac os App SwiftUI<\/p>\n<h1>\u4f7f\u7528\u65b9\u6cd5<\/h1>\n<ol>\n<li>\u5b9a\u4e49\u4e8b\u4ef6\u540d\u79f0\uff0c\u5e76\u5148\u6ce8\u518c\u4e00\u4e0b<\/li>\n<\/ol>\n<pre><code class=\"language-swift\">\/\/\u5b9a\u4e49\u4e8b\u4ef6\u540d\u79f0\uff0c\u540e\u9762\u4ee5\u5176\u4e3a\u6807\u5fd7\u63a5\u6536\u901a\u77e5\nstatic let SHOW_ALERT = &quot;ShowAlert&quot;\n\n\/\/OperationQueue.main\u4e3a\u4e3b\u7ebf\u7a0b\uff0c\u6536\u5230\u901a\u77e5\u4e4b\u540e\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u89e6\u53d1\uff0cnotification\u5c31\u662f\u81ea\u5df1\u6536\u5230\u7684object\nNotificationCenter.default.addObserver(forName: NSNotification.Name(NotificationEvent.SHOW_ALERT), object: nil,\n                                        queue: OperationQueue.main) {[self] notificationObj in\n    \/\/do something\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u53d1\u9001\u901a\u77e5\u4e8b\u4ef6\uff0c\u8fd9\u4e2a\u7b80\u5355\u4e0d\u7528\u591a\u8bf4<\/li>\n<\/ol>\n<pre><code class=\"language-swift\">NotificationCenter.default.post(name: NSNotification.Name(NotificationEvent.SHOW_ALERT), object: notificationObj)\n<\/code><\/pre>\n<h1>\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879<\/h1>\n<p>\u5728SwiftUI\u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528struct\u4f5c\u4e3a\u4e3bUI\u5bf9\u8c61\uff0cUI\u5bf9\u8c61\u7684Init\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\uff0c\u6240\u4ee5\u5c3d\u91cf\u4e0d\u8981\u5728Init\u4e2d\u6ce8\u518c\u901a\u77e5\uff0c\u4e0d\u7136\u4f1a\u53d1\u751f\u6536\u5230\u591a\u6b21\u901a\u77e5\u7684\u60c5\u51b5\uff0c\u5185\u5b58\u6cc4\u6f0f\u4ec0\u4e48\u7684\u3002<\/p>\n<p>\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u662f\u5728Ui\u663e\u793a\u65f6\u8ba2\u9605\u901a\u77e5\uff0c\u5728UI\u6d88\u5931\u65f6\u53d6\u6d88\u8ba2\u9605\uff1a<\/p>\n<pre><code>\/\/\u5728UI\u58f0\u660e\u4e4b\u540e\u6dfb\u52a0\n.onAppear(){\n    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(NotificationEvent.SHOW_ALERT), object: nil)\n    NotificationCenter.default.addObserver(forName: NSNotification.Name(NotificationEvent.SHOW_ALERT), object: nil,\n                                            queue: OperationQueue.main) {[self] notification in\n        notificationAction(notification:notification)\n    }\n}\n.onDisappear(){\n    NotificationCenter.default.removeObserver(self)\n}\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":209,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[22,7],"tags":[],"class_list":["post-208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swiftui","category-bolg"],"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jinhaihan.top\/WordPress\/wp-content\/uploads\/2022\/08\/pexels-photo-193003.jpeg?fit=1880%2C1253&ssl=1","_links":{"self":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":1,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":210,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts\/208\/revisions\/210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/media\/209"}],"wp:attachment":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}