IOS development-pass the value through the Protocol and the Delegate agent (Delegate)

Source: Internet
Author: User

IOS development-pass the value through the Protocol and the Delegate agent (Delegate)
Because Object-C does not support multi-inheritance, it is often replaced by Protocol. Protocol can define only one set of public interfaces, but it cannot provide specific implementation methods. That is to say, it only tells you what to do, but does not care about how to do it. When a class uses a Protocol (Protocol), it must comply with the Protocol. For example, if you do not implement some necessary implementation methods, the compiler will report a warning to remind you that you have not followed the ×× protocol. Note: Here I am talking about warnings, not errors. Yes, even if you don't implement the "necessary implementation" methods, the program can run, but there are some additional warnings. I will put this Demo at the end of this Article. If you need it, you can download it. Thank you. Role of Protocol (Protocol): 1. define a set of Public interfaces (Public) @ required: required methods @ optional: optional methods (not all can be implemented) 2. Delegate (Delegate) value transfer: it is a design pattern, which means entrusting others to do something. For example, Class A calls the method of class B and class B encounters A problem during execution. In this case, we need to use A proxy (Delegate ). Another example is the transfer of values between the Controller and the Controller, which redirects from C1 to C2 and then notifies C1 to update the UI or do other things when returning from C2 to C1, at this time, we will use the proxy (Delegate) to pass the value. 1. define a set of Public interfaces (Public) ProtocolDelegate. h code (the Protocol will not be generated. m file): # import <Foundation/Foundation. h> @ protocol ProtocolDelegate <NSObject> // required method @ required-(void) error; // optional implementation method @ optional-(void) other;-(void) other2;-(void) other3; @ end in the class to use the Protocol, import its header file: # import "ViewController. h "# import" ProtocolDelegate. h "Here I select the entry file. Remember to follow the protocol: @ interface ViewController () <ProtocolDelegate> @ end. A warning will be reported because the Defined Protocol contains One is a required method, but we have not implemented it: after the required method is implemented, the compiler will not report a warning: as for other optional methods, you can choose to implement, or none. II. The main class file code is released below the value transfer by the Delegate proxy (Delegate). I wrote a comment in it and you should be able to understand it. It doesn't matter if you don't understand it. I will put a Demo at the end of this article. ViewController. m file: # import "ViewController. h "# import" ProtocolDelegate. h "# import" ViewControllerB. h "@ interface ViewController () <ProtocolDelegate, scheme> @ end @ implementation ViewController-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {ViewControllerB * vc = segue. destinationViewController; [vc setDelegate: self];} // here the Protocol method for implementing controller B-(void) sendValue :( NSStr Ing *) value {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "success" message: value delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil] [alertView show];}-(void) error {}@ end ViewControllerB. h file: # import <UIKit/UIKit. h> // create a new protocol. The protocol name is generally set to "class name + Delegate" @ protocol ViewControllerBDelegate <NSObject> // The method for passing values by proxy-(void) sendValue :( NSString *) value; @ end @ interface ViewControllerB: UIViewController // The delegate. Generally, the proxy must use the weak reference (weak) @ property (weak, nonatomic) id <ViewControllerBDelegate> delegate; @ end ViewControllerB. m file: # import "ViewControllerB. h "@ interface ViewControllerB () @ property (strong, nonatomic) IBOutlet UITextField * textField; @ end @ implementation ViewControllerB-(IBAction) backAction :( id) sender {if ([_ delegate respondsToSelector: @ selector (sendValue :)]) {// if the protocol responds to s EndValue: method [_ delegate sendValue: _ textField. text]; // notification execution protocol method} [self. navigationController popViewControllerAnimated: YES];} @ end summary: When you need to define a set of public interfaces, you can use the Protocol to implement different methods. When you need to transfer values between classes, you can also use the proxy design mode to transfer values based on the Protocol.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.