Detailed description about Cocoa programming module collaboration

Source: Internet
Author: User

Cocoa programming moduleCollaboration is the content to be introduced in this article.Cocoa ProgrammingIn, we often encounter delegate, Target-Action, notification, and so on. In my opinion, we can sum up these methods from a certain perspective. They are just different.ModuleCollaboration methods are provided between them. Here I think of this collaboration as "Callback". In fact, the word "Callback" is more borrowed here, I have not carefully studied these just a bunch of callback functions. I just think it is very convenient to illustrate the problem, so I just want to borrow them, the common "Callback" in the application layer includes the following categories:

First, of course, it is a repeat of delegate. There is nothing to say about it. The tutorials and documents are very authoritative and carefully explained. They are often used in "Parent and Child" instances;

Second, this method is common, but it may not be considered as a "Callback", which is the Target-Action mode;

Third: the most labor-demanding NSNotificationCenter, which provides satisfactory services in the most comfortable way. It has three important keywords: observer, sender, and name, observer is the receiver of notification. It is often self in "registration method". I also suggest that you register your own notification.

Sender can see this when setting the object parameter posticationicationname: object: And postNotificationName: object: userInfo :) In "sending method". This parameter is very important and directly affects the quality of nsicationicationcenter, what does sender mean? It is notification's "signature". When registering a method, we can specify which sender's notification is accepted only, therefore, the center determines how to send the object based on the "signature" passed by the "method" parameter. The name is simple, which is equivalent to the title, the observer can and must specify which name notifications are accepted. Different names have different meanings. nsicationicationcenter is very useful between modules with relatively distant links;

Fourth: KVO (Key-Value Observing), which is rarely used. This method can be used when Module B is interested in the attribute status of module A. For more information, see: key-Value Observing Quick Start;

Fifth: An example shows that it directly transmits the pointer and method of an object as a parameter to another object and allows another object to call itself. This is the sentence:

 
 
  1. UIImageWriteToSavedPhotosAlbum(originalImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);  
  2. #pragma mark -  
  3. #pragma mark UIImagePickerController delegate  
  4. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)Info {      
  5.       
  6.     UIImage *originalImage = [Info objectForKey:UIImagePickerControllerOriginalImage];  
  7.     if(nil != originalImage)  
  8.     {  
  9.          UIImageWriteToSavedPhotosAlbum(originalImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);  
  10.     }  
  11.         ...  
  12. }  
  13.  
  14. - (void) image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {  
  15.     ...  

Summary: DetailsCocoa programming moduleAfter introducing the content of the collaboration, the above is my small summary.ModuleThe collaboration between them may be frequently used, and we hope to start learningCocoa ProgrammingSome help!

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.