The implementation of objective-C design pattern (MVC) and the application of protocols and Delegation

Source: Internet
Author: User

The interface is often used during iPhone development. Apple has designed a good management idea for it, that is, using the common MVC mode. In the iPhone, all graphical controls are inherited from uiview, that is, "V ". uiview and its Child classes are mainly responsible for UI implementation. All events generated by uiview can be delegated to uiviewcontroller for implementation. for different uiviews, the corresponding uiviewcontroller corresponds to "C" in MVC ". for "M", that is, the data model, it is left for the user to play.

Objective-C only supports single inheritance, which is similar to Java and can implement multiple protocols ).

First, define a view

1. @ interface myuiview: uiview {
2.
3.
4. // define some controls
5.
6. ID <myuiviewdelegate> delegate; // This definition will be explained later. It is a protocol used to implement delegation.
7.
8 .}
9.
10. // define some control settings
11. @ property id <myuiviewdelegate> delegate; // defines an attribute that can be used for get set operations.
12.
13. @ end

Define a protocol. According to cocoa's habits, it generally ends with delegate. Those familiar with C # Should know its meaning. in fact, whether it is an interface, a delegate, or a callback function, it basically does one thing. it defines an operation contract, and then the user can implement its specific content.

1 @ protocol myuiviewdelegate
2 // here you only need to declare the Method
3-(void) func1
4-(INT) func2 :( INT) ARG
5
6 @ end
 

After completing the above two steps, you need to design your own uiviewcontroller. in general, this controller can be used to implement the myuiviewdelegate defined above. in the cocoa framework, many controls and their controllers adopt this method.

1. @ interface myuiviewcontroller: uiviewcontroller <myuiviewdelegate>
2 .{
3. // member variables
4 .}
5.
6. // member methods, class methods, and attributes
7.
8. @ end

Now, you only need to declare the myuiview member variable in myuiviewcontroller and pass yourself as the delegate object to myuiview, so that you can become the proxy of myuiview, when an event occurs in myuiview, you can call the delegate method implemented in myuiviewcontroller.

It is very simple. Please complete the specific implementation and practice on your own.

By Mac-z

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.