{"id":52,"date":"2018-07-21T22:42:44","date_gmt":"2018-07-21T14:42:44","guid":{"rendered":"http:\/\/flykite.me\/?p=52"},"modified":"2018-07-21T23:00:32","modified_gmt":"2018-07-21T15:00:32","slug":"nsnotificationcenter%e7%a0%94%e7%a9%b6%e5%b0%8f%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/flykite.me\/?p=52","title":{"rendered":"NSNotificationCenter\u7814\u7a76\u5c0f\u8bb0"},"content":{"rendered":"<p>1.NSNotificationCenter\u5bf9observer\u7684\u5f15\u7528\u662f\u5f31\u5f15\u7528\uff0c\u5373\u5f15\u7528\u8ba1\u6570\u4e0d\u4f1a\u589e\u52a0\u3002<\/p>\n<p>2.\u5728iOS 9\u4e4b\u524d\uff0c\u8fd9\u4e2a\u5f15\u7528\u7c7b\u4f3c\u4e8eunsafe_unretained\uff0cobserver\u5bf9\u8c61\u88ab\u9500\u6bc1\u540e\u4e0d\u4f1a\u88ab\u7f6e\u4e3anil\uff0c\u56e0\u6b64\u63a5\u6536\u5230\u901a\u77e5\u65f6\u4f1a\u5bfc\u81f4\u91ce\u6307\u9488\u800c\u5d29\u6e83\u3002<\/p>\n<p>3.\u5728iOS 9\u4ee5\u4e0a\uff0c\u8fd9\u4e2a\u5f15\u7528\u7c7b\u4f3c\u4e8eweak\uff0c\u5373\u4f7f\u6ca1\u6709\u6267\u884cremoveObserver\u65b9\u6cd5\uff0c\u5728observer\u5bf9\u8c61\u88ab\u9500\u6bc1\u540e\u63a5\u6536\u901a\u77e5\u4e0d\u4f1a\u5f15\u53d1\u91ce\u6307\u9488\u5d29\u6e83\u3002<\/p>\n<p>4.UIViewController\u5bf9\u8c61\u4f1a\u5728dealloc\u65f6\u6267\u884cremoveObserver\u65b9\u6cd5\uff08\u672a\u786e\u8ba4\u662f\u5426\u8ddfiOS\u7248\u672c\u6709\u5173\uff09\u3002<\/p>\n<p>5.UIView\u5bf9\u8c61\u5728dealloc\u65f6\u4e0d\u4f1a\u6267\u884cremoveObserver\u65b9\u6cd5\uff0c\u76ee\u524d\u5df2\u77e5\u4f1a\u6267\u884cremoveObserver\u65b9\u6cd5\u7684\u4ec5\u6709UIViewController\u3002<\/p>\n<p>6.iOS\u5b98\u65b9\u6587\u6863\u5173\u4e8eaddObserver:selector:name:object:\u8fd9\u4e2a\u65b9\u6cd5\u7684\u63cf\u8ff0\u4e2d\u8bf4\u660e\u4e86\u201c\u5982\u679c\u4f60\u7684App\u7684\u8fd0\u884c\u5728iOS 9.0\u6216\u66f4\u65b0\u7248\u672c\u4e0a\u5219\u4e0d\u9700\u8981\u624b\u52a8\u79fb\u9664\u901a\u8fc7\u8be5\u65b9\u6cd5\u6dfb\u52a0\u7684\u89c2\u5bdf\u8005\uff08Observer\uff09\u5bf9\u8c61\u201d\uff0c\u8be6\u60c5\u8bf7\u770b\u5b98\u65b9\u6587\u6863\u7684Discussion\u90e8\u5206\u3002<\/p>\n<h2>\u5b98\u65b9\u6587\u6863<\/h2>\n<h3><strong>addObserver:selector:name:object:<\/strong><\/h3>\n<p>Adds an entry to the notification center&#8217;s dispatch table with an observer and a notification selector, and an optional notification name and sender.<\/p>\n<hr \/>\n<h3>Declaration<\/h3>\n<pre class=\"lang:objc decode:true \">- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSNotificationName)aName object:(id)anObject;<\/pre>\n<h3>Parameters<\/h3>\n<p><strong>observer<\/strong><br \/>\nObject registering as an observer.<\/p>\n<hr \/>\n<p><strong>aSelector<\/strong><br \/>\nSelector that specifies the message the receiver sends observer to notify it of the notification posting. The method specified by aSelector must have one and only one argument (an instance of NSNotification).<\/p>\n<hr \/>\n<p><strong>aName<\/strong><br \/>\nThe name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer.<\/p>\n<p>If you pass nil, the notification center doesn\u2019t use a notification\u2019s name to decide whether to deliver it to the observer.<\/p>\n<hr \/>\n<p><strong>anObject<\/strong><br \/>\nThe object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.<\/p>\n<p>If you pass nil, the notification center doesn\u2019t use a notification\u2019s sender to decide whether to deliver it to the observer.<\/p>\n<h3>Discussion<\/h3>\n<p>If your app targets iOS 9.0 and later or macOS 10.11 and later, you don&#8217;t need to unregister an observer in its dealloc method. Otherwise, you should call removeObserver:name:object: before observer or any object passed to this method is deallocated.<\/p>\n<blockquote><p>\n\u6b64\u5904\u7ffb\u8bd1\u4e00\u4e0b\uff1a<br \/>\n\u5982\u679c\u4f60\u7684App\u8fd0\u884c\u5728 iOS 9.0 \u4ee5\u4e0a\u6216 macOS 10.11 \u4ee5\u4e0a\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4e0d\u7528\u518d\u89c2\u5bdf\u8005\u5bf9\u8c61\u7684dealloc\u65b9\u6cd5\u4e2d\u5c06\u5176\u79fb\u9664\uff08\u5373\u8c03\u7528removeObserver\u65b9\u6cd5\uff09\u3002\u5426\u5219\uff0c\u4f60\u9700\u8981\u5728\u89c2\u5bdf\u8005\u5bf9\u8c61\uff08\u6216\u4efb\u610f\u88ab\u4f20\u5165\u5230\u8be5\u65b9\u6cd5\u4e2d\u7684\u5bf9\u8c61\uff09\u88ab\u9500\u6bc1\u65f6\u8c03\u7528removeObserver:name:object:\u65b9\u6cd5\u5c06\u5176\u79fb\u9664\u3002\n<\/p><\/blockquote>\n<p>\u53c2\u8003\uff1a<br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/a4d519e4e0d5\" target=\"_blank\" rel=\"noopener\">iOS NSNotificationCenter \u4f7f\u7528\u59ff\u52bf\u8be6\u89e3 &#8211; \u7b80\u4e66<\/a><br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/6a05f30cd605\" target=\"_blank\" rel=\"noopener\">NSNotificationCenter\u548cNSNotificationQueue\u5b66\u4e60\u7b14\u8bb0 &#8211; \u7b80\u4e66<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.NSNotificationCenter\u5bf9observer\u7684\u5f15\u7528\u662f\u5f31\u5f15\u7528\uff0c\u5373\u5f15\u7528\u8ba1\u6570\u4e0d\u4f1a\u589e\u52a0\u3002 2.\u5728iO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":1,"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":76,"href":"https:\/\/flykite.me\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions\/76"}],"wp:attachment":[{"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flykite.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}