IOS Protocol and Delegation

Source: Internet
Author: User
Learning a new knowledge with the established knowledge may get twice the result with half the effort, but sometimes it will be counterproductive, because our existing knowledge may be similar to the new one, there may also be some conceptual conflicts. In the Javascript blog, I have already said how I feel.
When I understand the conflicting things, it is called progress. We cannot use the existing knowledge to step to death, so we need to constantly absorb some fresh foreign blood.
Some concepts, ideas, and other things are not the same as the existing knowledge. I may be difficult at the early stage of my study. Sometimes, I may ask myself about unstable mood fluctuations, can't you learn this thing yourself? How can you be an idiot in front of this knowledge? If you are not afraid of learning or not, you will be afraid of not learning. If you take it seriously, you will not be able to stick to it for a while.
This idea. I have also used examples like Java and C #, because these two are very similar, so only one of them can be used, and it will not take much time to learn the other, but this is to get started, if it goes deeper, it will take more time to learn at the cost of time.
Tomorrow's New Year's Eve. Today I am watching iOS here. Although I insist on it, I am still inefficient. I have to read a few words and take a rest. I am still waiting for the time in the lower right corner of my computer, I keep saying, "I'm not off duty yet, will I leave early today.
No matter whether you are busy or idle, let yourself relax today, just hope to gain something in 2013.

IOS developers use object-C (OC), which is extended based on the C language. With the addition of object-oriented things, we know that there is another language, I know what you know about C ++, so many languages that learn OC will start from the development history. In fact, it makes sense to understand the development history,
We can know what things are available in that era and the functions of these things at that time. We can use our current knowledge to determine whether the things at that time are perfect and what is missing?
There are no templates in OC, no inheritance, no private method, no inline functions, and no virtual keywords. The protocol in OC is equivalent to the interface in C #, and the interface is equivalent to the class in C ++ ,. M file is equivalent. CPP file, the delegate is equivalent to the delegate in C #. The set in OC can only accommodate objects. The object construction in OC is divided into two parts,
Instead of a constructor with the same class name as C #/Java, the constructor can return an ID type. Now let's look at the protocols and delegation in OC.
The following content is from: Http://blog.csdn.net/jacky_jin/article/details/7004013

 An agreement means that, after using this agreement, we must follow this agreement, and the methods required by the Agreement must be implemented. As the name suggests, entrusting (proxy) means entrusting others to do things, that is, when one thing happens, it will not be handled by others.
1 . There are two types of protocols and delegation used at the same time. One is the Controller class, and the other is the view class. When you operate the view, you need to notify the Controller so that the controller can process some logic, the delegate and protocol controller are required. h # Import " View. h " @ Interface Controller: nsobject <testdelegate> {View * _ View ;} @ End Controller. m @ implement -( Void ) Viewdidload {View = [[View alloc] init]; view. Delegate = Self; // Delegation is used here, which means that the view will hand over the event to the Controller for handling. Embodies the idea of MVC } // View does not actually implement testmethod1 and testmethod2. -( Void ) Testmethod1 { // Deal Logic } -( Void ) Testmethod2 { // Deal Logic } View. h @ Protocol Testdelegate -( Void ) Testmethod1 -( Void ) Testmethod2 @ End @ Interface View: nsobject { ID <Testdelegate> Delegate ;} View. M # implement // Process some user events on the View -( Void ) Onaction1 {[ Delegate Testmethod1];} -(Void ) Onaction2 {[ Delegate Testmethod2];} @ End 2 The nsobject class can be viewed separately using the Protocol. This is the base class of all objectc classes and it implements <Nsobject> When the Protocol has an nsstring class * STR; can be [STR retain]; retain is a method specified in the nsobject protocol, but not a member method of the nsobject class. I believe there are -( ID ) Retain { // Some code Return Self;} The nsobject class will implement this method, because this method is defined in the nsobject protocol. Learned C ++ Will understand that the protocol here is a bit like an abstract class in C ++. Will this help you understand?

 

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.