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