Use of iOS---agents and protocols, and notifications
Last Update:2017-06-24
Source: Internet
Author: User
<span id="Label3"></p><p><p>first, the agent</p></p><p><p>1. Introduction of the Agent</p></p><p><p>Proxy is a common design pattern</p></p><p><p>Agent Usage: A let B do something, word ..., sign an agreement.</p></p><p><p>So the agent has three components:</p></p><p><p>Principal: Define Agreement</p></p><p><p>Agreement: used to stipulate what the agent can do and what must be done</p></p><p><p>Agent: according to the agreement to complete the requirements of the entrusted party</p></p><p><p>2. Introduction of the agreement</p></p><p><p>A protocol is a common set of interfaces, a list of methods, but not implemented.</p></p><p><p>You can implement the methods in the protocol through the Proxy.</p></p><p><p>A protocol is a common method, usually written in a class.</p></p><p><p>If multiple classes use this protocol, they can be written as a peotocol file.</p></p><p><p>3. Use of agents</p></p><p><p>(1) entrust Sb. to do Sth.</p></p><p><p>First build a custom class, custom class to buy things, set up a protocol, the realization of the agreement, the implementation of the custom protocol Method.</p></p><p><p>Custom.h</p></p><pre><pre>#import <Foundation/Foundation.h><span style="color: #000000"></span><NSObject><span style="color: #000000">@required</span>-(<span style="color: #0000ff">void</span>) custom: ( Customer*) Customer buythingnum: (<span style="color: #0000ff">int</span><span style="color: #000000">) count; @end @interface Customer:nsobject@property (nonatomic,weak) ID</span><CustomerDelegate><span style="color: #0000ff">delegate</span><span style="color: #000000">;</span> -(<span style="color: #0000ff">void</span>) buythingnum: (<span style="color: #0000ff">int</span><span style="color: #000000">) count; @end</span></pre></pre><p><p></p></p><p><p>Custom.m</p></p><pre><pre><span style="color: #800000">"</span> <span style="color: #800000">Customer.h</span> <span style="color: #800000">"</span> <span style="color: #000000">@implementation Customer</span>-(<span style="color: #0000ff">void</span>) buythingnum: (<span style="color: #0000ff">int</span><span style="color: #000000">) count{ </span><span style="color: #0000ff">if</span> (self . <span style="color: #0000ff">delegate</span> && [self. <span style="color: #0000ff"> Delegate</span> <span style="color: #000000"> respondstoselector: @selector (custom:buythingnum:)]) { [self.</span> <span style="color: #0000ff">delegate</span> custom:self buythingnum:<span style="color: #800080">5</span><span style="color: #000000">];} } @end</span></pre></pre><p><p>Viewcontroller.m</p></p><pre>#import<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">ViewController.h</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">#import</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">Customer.h</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">@interface Viewcontroller ()</span></span><CustomerDelegate><span style="color: #000000"><span style="color: #000000">@end @implementation Viewcontroller</span></span>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Viewdidload {[super viewdidload]; Customer</span></span>*custom =<span style="color: #000000"><span style="color: #000000">[Customer alloc]init]; Custom.</span></span><span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span>=<span style="color: #000000">self <span style="color: #000000">; [custom buythingnum:</span></span><span style="color: #800080"><span style="color: #800080">5</span></span><span style="color: #000000"><span style="color: #000000">];}</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) custom: (customer*) Customer buythingnum: (<span style="color: #0000ff"><span style="color: #0000ff">int</span></span><span style="color: #000000"><span style="color: #000000">) Count{NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">%d</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">, count);} @end</span></span></pre><p><p>(2) Reverse Transfer value</p></p><p><p>Defines a Customviewcontroller class that defines the protocol in CUSTOMVIEWCONTROLLER.H.</p></p><pre><pre>#import <UIKit/UIKit.h><span style="color: #000000"></span><NSObject><span style="color: #000000">@required</span>-(<span style="color: #0000ff">void</span>) buythingnum: (<span style="color: #0000ff">int </span> <span style="color: #000000">num; @end @interface customviewcontroller:uiviewcontroller@property (nonatomic,weak) ID</span>< Passvaluedelegate><span style="color: #0000ff">delegate</span><span style="color: #000000">; @end</span></pre></pre><p><p></p></p><p><p>Define a method in customviewcontroller.m, if the proxy is set up, and the proxy implements the method of the protocol, then the proxy method is Executed.</p></p><pre>#import<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">CustomViewController.h</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">@interface customviewcontroller () @end @implementation Customviewcontroller</span></span>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Viewdidload {[super viewdidload]; Self.view.backgroundColor</span></span>=<span style="color: #000000"><span style="color: #000000">[uicolor orangecolor];}</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent *)<span style="color: #0000ff"><span style="color: #0000ff">Event</span></span><span style="color: #000000"><span style="color: #000000">{ </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(self.)<span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span>&& [self.<span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span><span style="color: #000000"><span style="color: #000000">respondstoselector: @selector (buythingnum:)]) {[self.</span></span><span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span>Buythingnum:<span style="color: #800080"><span style="color: #800080">5</span></span><span style="color: #000000"><span style="color: #000000">]; }} @end</span></span></pre><p><p>In the Viewcontroller class, set up the proxy to implement the proxy method.</p></p><pre>Import<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">ViewController.h</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">#import</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">CustomViewController.h</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">@interface Viewcontroller ()</span></span><PassValueDelegate><span style="color: #000000"><span style="color: #000000">@end @implementation Viewcontroller</span></span>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Viewdidload {[super viewdidload]; Self.view.backgroundColor</span></span>=<span style="color: #000000"><span style="color: #000000">[uicolor yellowcolor];}</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent *)<span style="color: #0000ff"><span style="color: #0000ff">Event</span></span><span style="color: #000000"><span style="color: #000000">{customviewcontroller</span></span>*CUSTOMVC =<span style="color: #000000"><span style="color: #000000">[[customviewcontroller alloc]init]; Customvc.</span></span><span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span>=<span style="color: #000000">self <span style="color: #000000">; [self Presentviewcontroller:customvc Animated:yes completion:nil];}</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) Buythingnum: (<span style="color: #0000ff"><span style="color: #0000ff">int</span></span><span style="color: #000000"><span style="color: #000000">) Num{NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">%d</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">, num);} @end</span></span></pre><p><p></p></p><p><p>TWO. Notice</p></p><p><p>1. Sending of Notifications: the system is through a call center, Notification Center is a singleton.</p></p><pre><pre> Nsnotification *notifacation = [nsnotification notificationwithname:<span style="color: #800000">@ "</span><span style="color: #800000">passvalue</span><span style="color: #800000">"</span> <span style="color: #0000ff"> Object</span>:<span style="color: #800000">@ "</span> <span style="color: #800000">haha</span> <span style="color: #800000">"</span> userinfo:@{<span style="color: #800000">@ "</span><span style="color: #800000">num</span><span style="color: #800000">"</span>:<span style="color: #800000">@ "</span> <span style="color: #800000">Ten</span> <span style="color: #800000">"</span> <span style="color: #000000">}]; [[nsnotificationcenter defaultcenter]postnotification:notifacation];</span></pre></pre><p><p>2. Receiving Notifications</p></p><pre>- (<span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">) Viewdidload {[super viewdidload]; Self.view.backgroundColor</span></span>=<span style="color: #000000"><span style="color: #000000">[uicolor yellowcolor]; [[nsnotificationcenter defaultcenter]addobserver:self selector: @selector (buythingnum:) name:</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">Passvalue</span></span><span style="color: #800000"><span style="color: #800000">"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Object</span></span><span style="color: #000000"><span style="color: #000000">: nil];}</span></span>-(<span style="color: #0000ff"><span style="color: #0000ff">void</span></span>) buythingnum: (nsnotification *<span style="color: #000000"><span style="color: #000000">) noti{nsstring</span></span>*wordstr = [noti<span style="color: #0000ff"><span style="color: #0000ff">Object</span></span><span style="color: #000000"><span style="color: #000000">]; Nsdictionary</span></span>*dic =<span style="color: #000000"><span style="color: #000000">[noti userInfo];<br>Get the value of the notification<br>NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">%@</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">, wordstr); NSLog (</span></span><span style="color: #800000"><span style="color: #800000">@"</span></span><span style="color: #800000"><span style="color: #800000">%@</span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">, dic);<br>}</span></span></pre><p><p>3. Remove Notifications</p></p><pre><pre> [[nsnotificationcenter defaultcenter] removeobserver:self name:<span style="color: #800000">@ "</span><span style="color: #800000">passvaule</span><span style="color: #800000">"</span> <span style="color: #0000ff"> Object</span>: self];</pre></pre><p><p></p></p><p><p>Printing results:</p></p><p><p><span style="font-size: 12px">2017-06-24 21:53:01.021 delegatetest[15607:2194987] haha</span></p></p><p><p><span style="font-size: 12px">2017-06-24 21:53:01.021 delegatetest[15607:2194987] {</span></p></p><p><p><span style="font-size: 12px">num = 10;</span></p></p><p><p><span style="font-size: 12px">}</span></p></p><p><p><span style="font-size: 12px"><strong></strong></span></p></p><p><p><span style="font-size: 14px">Iii. advantages and disadvantages of notice and agent</span></p></p><p><p>(1) notification can be a One-to-many communication, agent can only one.</p></p><p><p>(2) the execution efficiency of the agent is relatively high.</p></p><p><p>(3) the use of the notification is relatively simple.</p></p><p><p>(4) The code is more difficult to maintain if there are too many Notifications.</p></p><p><p></p></p><p><p>Use of iOS---agents and protocols, and notifications</p></p></span>