Proxies are a common design pattern in iOS development. We can easily implement this design pattern with the help of Protocol (see Blog: Objective-c Protocol (Protocol)).
What is an agent?
Apple's official documentation gives a clear explanation:
delegation is a-simple and powerful-pattern in which one object-a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object-the Delegate-and at the appropriate time sends a message to it . The message informs the delegate of an event, the delegating object is about to handle or have just handled. The delegate respond to the message by updating the appearance or state of itself or other objects in the application, And in some cases it can return a value this affects how an impending event is handled. The main value of delegation is the it allows-easily customize the behavior of several objects in one central Obje Ct.
It is a simple and powerful design pattern for an object to "represent" another object and interact with other objects in the program. The main object (in this case, delegating object) maintains a reference to a proxy (delegate) and sends a message to the agent at the appropriate time. This message informs the "agent" that the main object is about to be processed or that an event has been processed. The agent can respond to the message of the event sent by the main object by updating the UI interface or other state of the object. Or, in some cases, return a value to affect how other upcoming events should be handled. The main value of the agent is that it allows you to easily customize the behavior of various objects. Note that the agent here is a noun, which is itself an object that is specifically representative of the object being proxied to deal with other objects in the program.
Cocoa agents in the
The Cocoa touch framework is heavily used in proxy design patterns, where each UI control class declares a type ID of delegate or datasource, and looking at Cocoa's header file can find many of the following properties:
@property(nonatomic, assign) ID<uiactionsheetdelegate> delegate; //Weak reference
The usual format is @property(nonatomic, assign) ID<protocol_name> delegate; That is, the agent should follow a certain protocol, that is, only the class object that follows the agreement is eligible for proxy. This also requires that the proxy class must declare the @required method in the header file to follow this Protocol_name protocol and implement it, @optional method is optional.
Take Uiactionsheet as an example, we define a view that triggers uiactionsheet when a button in the view is clicked, when the user completes an action on uiactionsheet, such as the destruct button being pressed, Or the Cancel button is pressed, Uiactionsheet sends a message to delegate, and the delegate completes the response to the user's action, such as printing a string to the screen. The diagram illustrates the following:
First, we create a tab-based project that adds code to the FirstViewController.h, which follows the Uiactionsheetdelegate protocol:
1
Add a button to the view to trigger the Actionsheet, and then write the response code for the button:
1-(ibaction) Invokeactionsheet: (ID) Sender {2 3Uiactionsheet *actionsheet =[[Uiactionsheet alloc]4Initwithtitle:@"Delegate Example" 5 Delegate: Self//telling this class (Viewcontroller) to implement Uiactionsheetdelegate6Cancelbuttontitle:@"Cancel" 7Destructivebuttontitle:@"Destruct" 8Otherbuttontitles:@"Button 1",@"Button 2", nil]; 9 Ten [Actionsheet ShowInView:self.tabBarController.view]; One [Actionsheet release]; A}
Note that one of the important settings above is that there is a delegate:self in the parameter, which indicates that the Uiactionsheet agent is self, or firstviewcontroller.
Then implement the methods in the Uiactionsheetdelegate in FIRSTVIEWCONTROLLER.M:
1 #pragmaMark--uiactionsheet Delegate Methods2- (void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex {3 Switch(buttonindex) {4 Case 0: 5Self.myTextFromActionSheet.text =@"Action destructed!"; 6 Break; 7 Case 1: 8Self.myTextFromActionSheet.text =@"Action Button 1 clicked!"; 9 Break; Ten Case 2: OneSelf.myTextFromActionSheet.text =@"Action Button 2 clicked!"; A Break; - Case 3: -Self.myTextFromActionSheet.text =@"Cancel Button clicked!"; the Break; - default: - Break; - } + -}
In the above steps we have completed the use of cocoa in the uiactionsheet already have agents. However, many times we need to write our own custom agent, how to do it?
Custom Proxy
What we're going to do is create a view and customize a proxy implementation to update the string in this view. Above we have created a tab project to borrow the second view inside. We drag a button to the above named Changetext, the response function is-(ibaction) Changetext: (ID) sender; Click this button to enter a modal view named Changetextview, We enter a string in Changetextview and update the string to second view after exiting the view. How do you implement data transfer between modal view and second view? That's the agent! Who's acting? Changetextview's agent! Because we enter data directly in the Changetextview, the input string needs to be fed back to the second view by the agent.
1. Create a new class Changetextviewcontroller and create the corresponding xib file.
2. Declare an agreement in ChangeTextViewController.h changetextviewdelegate:
1 @protocol changetextviewdelegate <NSObject> 2 3 -(void) Textentered: (nsstring*) text; 4 5 @end
Like Uiactionsheet, in Changetextviewcontroller we also need to add a proxy declaration:
1 Delegate
3, we also need to add a button in the Changetextviewcontroller.xib save, when the button is pressed back to second view, and update the string. The response function for the Save button is:
1-(ibaction) savebuttonclicked: (ID) Sender {2 //is anyone listening3 if([Delegaterespondstoselector: @selector (textentered:)])4 { 5 //send the delegate function with the amount entered by the user6[DelegateTextEntered:textEntered.text]; 7 } 8 9 [self dismissmodalviewcontrolleranimated:yes]; Ten}
[Delegate textentered: textentered. Text]; The meaning of this code is Changetextviewcontroller notification agent, textentered This event occurred, to textentered the realization of this message, That is, how to respond to this textentered event is implemented by the proxy. In this case, Secondviewcontroller is the proxy for the Changetextviewcontroller object. So, we need to set the Secondviewcontroller to meet the agent's conditions. First, the declaration in SecondViewController.h follows the protocol changetextviewdelegate. Then edit the response function of the Changetext button-(ibaction) Changetext: (ID) sender;
1-(ibaction) Changetext: (ID) Sender {2Changetextviewcontroller *ctviewcontroller = [[Changetextviewcontroller alloc] Initwithnibname:@"Changetextviewcontroller"Bundle:nil]; 3 //Assign This class to the delegate of Changetextviewcontroller,4 //Remember to make Thie Viewcontroller confirm to Protocol "Changetextviewdelegate"5 //which is delared in file ChangeTextViewController.h6Ctviewcontroller.Delegate=Self ; 7 [self presentmodalviewcontroller:ctviewcontroller animated:yes]; 8}
Note that ctviewcontroller.delegate = self, which implements Secondviewcontroller as a proxy for Changetextviewcontroller objects.
"Turn from: http://blog.csdn.net/lovefqing/article/details/8270111"
iOS Development agent (delegate)