Viewer mode for iOS design mode
Last Update:2015-07-30
Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">Viewer mode <span style="font-family:Helvetica">:</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="font-family:Helvetica"><span style="white-space:pre"></span></span>Defines a one-to-many dependency between objects <span style="font-family:Helvetica">,</span> and when an object's state changes <span style="font-family:Helvetica">,</span> all objects that depend on it are notified and automatically updated <span style="font-family:Helvetica">.</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:22px"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:22px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">When to use Observer mode</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">(1): changes to an object need to change other objects at the same time, without knowing how many objects remain to be changed <span style="font-family:Helvetica"> <span style="white-space:pre"></span> </span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>(2): an object must notify other objects, and he does not need to know what other objects Are.</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>(3): There are two types of abstraction that depend on each other and encapsulate them in their respective objects, so that they can be individually changed and Reused. <span style="font-family:Helvetica"> </span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:22px"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:22px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"><span style="font-size:18px"><span style="font-family:‘Heiti SC Light‘">in the</span> <span style="font-family:‘Heiti SC Light‘">using observer mode in the</span> Cocoa Touch frame</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:22px"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:22px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">1: notification type <span style="font-family:Helvetica">:</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="font-family:Helvetica"><span style="white-space:pre"></span></span>Use <span style="font-family:Helvetica">nsnotificationcenter</span> and <span style="font-family:Helvetica">nsnotification</span> objects to achieve <span style="font-family:Helvetica"></span> a one-to-many interaction <span style="font-family:Helvetica">that</span> allows the target and observer to communicate in a coupled way <span style="font-family:Helvetica">, </span>and the two don't need much understanding.</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">For example, a model constructs a notification to be placed in the notification Center:</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>Nsnotification *notification = [nsnotification notificationwithname:@ "data change" object:self];</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>Nsnotificationcenter *notificationcenter = [nsnotificationcenter defaultcenter];</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>[notificationcenter postnotification:notification];</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">Any object to subscribe to this notification, you first need to register for yourself</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>[notificationcenter addobserver:self</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>Selector: @selector (update:)</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>name:@ "data change"</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>object:subject];</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘; min-height:19px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px">2: Key-value Observation</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-family:‘Heiti SC Light‘"><span style="font-size:18px"><span style="white-space:pre"></span>This mechanism is based on the nskeyvalueobserving informal protocol, which cocoa provides an automated attribute-observing capability for all compliance Objects. KVC based on the relevant informal protocol, automatic observation by accessing object properties</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"><br></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:14px"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:14px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:14px"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica; min-height:14px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(29,148,33)"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(29,148,33)"><span style="font-size:18px">/* <span style="font-family:‘Heiti SC Light‘">Register Observer</span> */</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(61,29,129)"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(61,29,129)"><span style="font-size:18px"><span style="color:#000000">[</span><span style="color:#539aa4">_walker</span><span style="color:#000000"></span>addobserver<span style="color:#000000">:</span><span style="color:#c32275"></span><span style="color:#000000"></span>self forkeypath<span style="color:#000000">:</span><span style="color:#c91b13">@ ' age ' options:</span> nskeyvalueobservingoptionnew<span style="color:#000000"></span>context<span style="color:#000000">:</span><span style="color:#c32275">nil</span> <span style="color:#000000"></span> <span style="color:#000000"></span> <span style="color:#000000"> ];</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; min-height:21px"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; min-height:21px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(29,148,33)"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; color:rgb(29,148,33)"><span style="font-size:18px">/* KVO <span style="font-family:‘Heiti SC Light‘">Functions Call this function</span> <span style="font-family:‘Heiti SC Light‘">as long as</span> the KeyPath property <span style="font-family:‘Heiti SC Light‘">of</span> object <span style="font-family:‘Heiti SC Light‘">changes</span> */</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; min-height:21px"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo; min-height:21px"><span style="font-size:18px"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-family:Menlo"><span style="font-size:18px">-(<span style="color:#c32275">void</span>) observevalueforkeypath: (<span style="color:#6122ae">nsstring</span> *) keypath ofobject: (<span style="color:#c32275">ID</span>) object change: ( <span style="color:#6122ae"> Nsdictionary</span> *) Change context: (<span style="color:#c32275">void</span> *) context</span></p></p><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Viewer mode for iOS design mode</p></p></span>