IOS Protocol-CS193P Course Notes (1 ),

Source: Internet
Author: User

IOS Protocol-CS193P Course Notes (1 ),

1. Introduction of protocal and delegate

Because of the use of the "id" type, you cannot obtain the data type that you really want to pass or receive through the id (although you can ask at runtime through the id introspection mechanism ). Therefore, we will explain in the Code what methods should be implemented for this id type object (syntax definition, protocal or not, the code generated by the compiler is no different ).

Id <Protocal> obj

Object obj declared is an id-type object that meets certain conditions (Protocal ).

Id <Protocal> obj = [[MyClass alloc] init]

MyClass promises to implement the methods in Protocal.

 

Ii. protocal applications in iOS

1 delegate and sponse

If an object in iOS wants to complete some important functions and non-class methods, it will delegate the operations of these events to its proxy. To achieve the above purpose, this object declares an attribute that satisfies a series of methods (protocal), representing its proxy.

1.1 This is an alternative to subclassing to provide non-generic behavior)

Example:

1 // NetWorkManager. h 2 @ protocol NetworkDelegate 3 @ required 4-(void) task :( NetWorkFlag) tag didSuccess :( NSDictionary *) result withInfo :( NSString *) infoString; // success callback 5-(void) task :( NetWorkFlag) tag didFailed :( NSString *) failedReason; // failure callback 6 @ optional 7-(void) setProgress :( float) newProgress; 8 @ end 9 @ interface NetWorkManager: NSObject <ASIHTTPRequestDelegate> 10 @ property (nonatomic, weak) id <NetworkDelegate> networkDelegate; 11 ..... // other class interfaces

The above Code indicates that NetWorkManager proxies didSuccess and didFailed methods, and implements the NetworkDelegate method by its proxy id <networkDelegate>. NetworkDelegate can be an object of any type (id), and it promises to implement @ protocol NetworkDelegate.

1  //GetOrderController.h2  @interface GetOrderViewController : TTBaseViewController <UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,NetworkDelegate> 
1 // GetOrderController.m2 3 [self.networkManager setNetworkDelegate:self];

 

1.2 perform blind communication directly on the object (MVC)

For example, a View cannot have its own data. You need to constantly ask other objects for communication. View does not want to be bound to a specific type of object, so the communication object should be an id (invisible ).

GetOrderController has the tableView attribute, which is used to display the list information. That is, GetOrderController promises to implement the method in the merge se protocol. In this case, tableView communicates with GetOrderController (controller and view), that is, blind communication (blind ). The communication content here is count and dataAt.

 

 

 

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.