Recently saw an agent of the use of the document, feel good writing, record
1. Create a delegate;
2. A delegate is declared by the principal;
3. The principal invokes the method within the delegate;
4. The delegate is set by the principal so that it can be called by the entrusted person;
5. The entrusted person realizes the method defined by delegate.
1.1 First step: Create a delegate
In the. h file, create a delegate by @protocol:
@protocol calculatorprogramstableviewcontrollerdelegate
@optional
-(void) Calculatorprogramstableviewcontroller:
(Calculatorporgramtableviewcontroller *) sender
Choseprogram: (ID) program;
@end
Code Explanation:
@protocol calculatorprogramstableviewcontrollerdelegate
Used to create a delegate. There is one method in this delegate:
(void) Calculatorprogramstableviewcontroller
@optional shows that this method is not required to be implemented and is optional.
Tips:
It is necessary to define a concept here, although a delegate is defined by @protocol, but it cannot be said that delegate is protocol. As mentioned earlier, delegate is an architectural design pattern. In iOS, it is implemented through @protocol.
1.2 Step Two: The delegate declares a delegate
In the TableView. h file (CalculatorProgramsTableViewController.h), declare the previously created delegate through @property. The code is as follows:
@interface Calculatorprogramstableviewcontroller:uitableviewcontroller
...
Define a property delegate
@property (nonatomic, weak) id<calculatorprogramstableviewcontrolerdelegate>
Delegate
...
@end
After adding these lines of code, TableView has the delegate. With the delegate, TableView can send a message. It is only possible to send messages, but not yet. Next, Tableview begins sending messages.
1.3 Third Step: The delegate calls the method within the delegate
Our goal: When choosing a different function on TableView, TableView the selected function to inform the graphicview of the drawing function graph.
In this step, TableView sends a message by calling the delegate method. The code is implemented as follows:
#progma mark-uitableviewdelegate
-(void) TableView: (UITableView *) TableView
Didseelectrowatindexpath: (Nsindexpath *) Indexpath
{
ID program = [self.programs ObjectAtIndex:indexPath.row];
[Self.delegate calculatorprogramstableviewcontroller:self
Choseprogram:porgram];
}
Code Explanation:
Notice the self.delegate of the highlighted part. UITableView is sending messages through this previously defined delegate. This is done by Uitableviewcontroller calling the function defined in delegate. By the call of this function, the message is sent. But where it went, it was unclear. That's because the self.delegate hasn't been assigned yet.
Next, do this: set Self.delegate to Graphicview.
1.4 The delegate is set by the principal in order to be called by the entrusted person;
As mentioned earlier, the self.delegate in Uitableviewcontroller has not been assigned a value. Since TableView wants to pass the value to Graphicview, it should set the delegate in the Calculatorgraphviewcontroller.m file. In other words, let Graphicview become the delegate of TableView.
The code is as follows:
@implementation Calculatorgraphviewcontroller
...
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue
Sender: (ID) sender
{
if ([Segue.identifier isequaltostring:@ "Show favorite Graphics"]) {
Nsarray * programs = [[Nsuserdefaults standarduserdefaults]
Objectforkey:favorites_key];
[Segue.destinationviewcontroller Setprograms:programs];
[Segue.destinationviewcontroller setdelegate:self];
Set delegate
}
}
Code Explanation:
But when you click the button in the upper-right corner of the Graphicview, a tableview pops up. Also, in the last line of this code, set Calculatorgraphviewcontroller to TableView delegate.
With this setting, TableView can invoke the delegate method that Graphicview follows.
Delegate This method has not been realized, and quickly realize it.
1.5 The delegated person implements the method defined by delegate.
Remember that mysterious <>. Use the following code to let Graphicviewcontroller follow this delegate.
. h to implement the Protocol
@interface Calculatorgraphviewcontroller:nsobject
<CalculatorProgramsTableViewControllerDelegate>
...
@end
This is the last step in delegate, and we implement the method defined by this protocol in the controller file of the segue. The code is as follows:
Implement Delegate method
-(void) Calculatorprogramstableviewcontroller: (Calculatorprogramstableviewcontroller *) sender
Chooseprogram: (ID) program
{
Self.calculatorprogram = Program;
}
1.6 Summary
Through the demonstration of the above delegate five, we are clear about the mechanism of delegate. Delegate enables data interaction between different views. Delegate belongs to the event-driven category. Delegate is called only when an event is triggered.
In the cocoa framework, although there are many ways to store and access data, there is no alternative to delegate's unique data interaction patterns.
Note: The above comes from the Stanford iOS tutorial. This is a classic example of a delegate application.
Specific use of IOS agents