CocoaDevelopmentFrameworkLearning callbackFunctionThis is what we will introduce. Before reading this article, if you are not familiar with this article, please read the MVC Architecture Analysis of Cocoa Study Notes. Let's take a look at the content of this article.
CocoaDevelopment on Mac OS and iPhone OSFrameworkUse Objective-C as the development language. Of course, C and C ++ statements can also be embedded in the code. When I first got to know Objective-C, I thought it was a strange syntax, but it was essentially similar to other object-oriented languages. For more information about the syntax, see related documents.
In the Cocoa framework:
All controls and windows are inherited from UIView and correspond to V in MVC. UIView and its Child classes are mainly responsible for UI implementation, and events generated by UIView can be delegated to UIViewController for implementation. For different uiviews, the corresponding UIViewController corresponds to C in MVC. For example, UITableView, which is commonly used on iPhone OS, corresponds to the UITableViewController. As for M in MVC, it needs to be implemented according to the user's own needs.
Next, let's take a look at the architecture of the next custom UIView. Before that, let's take a look at several important keywords in Objective-C.
@ Interface defines a class, which is easily confused with interfaces in Java.
@ Protocol defines a protocol. I like to understand it as an interface, which is equivalent to an interface in Java.
Objective-C only supports single inheritance, but can implement multiple protocols (interfaces). The syntax is as follows:
- @ Interface Child: Parent <Protocol1, Protocol2> {// member variable definition} // member method, class method, attribute definition @ end
With the above knowledge, we can define our own UIView.
First, define a subclass of UIView.
- @ Interface MyUIView: UIView {
- // Define some controls
- Id <MyUIViewDelegate> delegate;
- // This definition will be explained later. It is a protocol used to implement delegation.
- }
- // Define some control settings
- @ Property id <MyUIViewDelegate> delegate;
- // Defines an attribute that can be used for get set operations.
- @ End
Define a Protocol. According to Cocoa's habits, it generally ends with a delegate. Students 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.
- @ Protocol MyUIViewDelegate // you only need to declare the method-(void) func1-(int) func2 :( int) arg @ end
After completing the preceding 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 use this method.
- @ Interface MyUIViewController: UIViewController <MyUIViewDelegate> {// member variable} // member method, class method, attribute @ end
The definition has been completed, but we cannot see how these three are linked up here. Next, let's take a look at the specific implementation of MyUIView and MyUIViewController.
The first is the implementation code of MyUIView. Assume that MyUIView will call the doSometing method after an event occurs.
- -(Id) init {MyUIView * myView = [[MyUIView alloc] init];
- // Initialize MyUIView. delegate = self;
- // Use the instance of MyUIViewController as the delegate object
- Self. view = myView;
- }
- -(Void) func1 {
- // For specific implementation, you can add Model-related code
- }
In the above Code, nil is equivalent to null in Java. The meaning here is very obvious. If the delegate object of delegate is not empty, call the corresponding method, but where can I implement this delegate object method. Let's take a look at the definition of MyUIViewController, which implements MyUIViewDelegate. So of course this method is implemented by it. The Code is as follows:
- -(Id) init {MyUIView * myView = [[MyUIView alloc] init];
- // Initialize MyUIView. delegate = self;
- // Use the instance of MyUIViewController as the delegate object
- Self. view = myView;
- }
- -(Void) func1 {
- // For specific implementation, you can add Model-related code
- }
In this way, the entire code line will be understood.
1. MyUIViewController Initialization
2. MyUIViewController initializes MyUIView during initialization and assigns itself as a delegate object to MyUIView
3. When a MyUIView event occurs, call callback) the method of the delegate object is actually the method of calling MyUIViewController.
Summary:CocoaDevelopmentFrameworkLearning callbackFunctionI hope this article will help you.