Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the design pattern of MVC framework in iPhone

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to parse the MVC framework design pattern in iPhone? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Collate from the open course on iphone development at Stanford University, and add some of your own understanding.

I. concept

Model = what is your application, which is an unrelated part of the user interface

Controller = how your application displays Model to users (UI logic), the center is the bridge, connecting Model and View

View = Controller's minions (minion) and minions. View obeys the command of Controller and reports important events to Controller in time.

II. Communication

1. Model and View can never communicate with each other, but can only be transmitted through Controller.

2. Controller can talk to Model directly (read and write call Model), and Model communicates indirectly with Controller through Notification and KVO mechanisms.

3. Controller can talk to View directly (through outlet, directly manipulate View,outlet to directly correspond to the controls in View), and View reports the occurrence of events to Controller through action (such as the user Touch me). Controller is the direct data source for View (the data is most likely taken from Model and processed by Controller). Controller is the delegate of View to synchronize View and Controller,delegate is a set of protocols that indicate that the View is adjusted to account to the user when the program is about to or has been in a certain state. For example, if the system is out of memory, do you reduce the quality of view to save memory?

Note: a delegate suddenly appears, which is hard to understand. In fact, it does not correspond to the XXAppDelegate file that xcode created for us. This file does not belong to any part of MVC, although it is associated with MVC. I found that when it is said in the Apple document that An is the agent of B, it usually means that A has a reference to B, and A can manipulate B directly.

III. Implementation

Setting up a BtnClick project system will generate the following files for us:

BtnClickAppDelegate.h

BtnClickAppDelegate.m

The above two files define that the BtnClicAppDelegate,UIApplicationDelegate-like protocol that implements the UIApplicationDelegate protocol is a predefined protocol of the system, which is responsible for monitoring the high-level behavior of the application, dealing with several key system messages, and is the Hook reserved for us in the application life cycle, which is essential for every iphone application. The life cycle of an iphone application:

BtnClickViewController.h

BtnClickViewController.m

The implementation of Controller. You can define some IBOutlet elements and IBAction methods to communicate with View.

Interface BtnClickViewController: UIViewController {IBOutlet UILabel* statusText;} @ property (retain,nonatomic) UILabel* statusText;-(IBAction) buttonPressed: (id) sender; @ end

Which file does View correspond to? The answer is MainWindow.xib and BtnClickViewController.xib under Resource. The MainWindow.xib file is automatically loaded when the application loads, which is actually configured in the plist file. MainWindow.xib then loads the child view BtnClickViewController.xib.

Why is it named ViewController instead of naming it separately? Maybe it's because View and Controller are too close, and view is Controller's sidekick. When actually programming, you must distinguish the responsibilities of each part of MVC.

I haven't seen Model from beginning to end, but M is optional, especially for simple applications. Anything that has nothing to do with the interface, the custom classes we add to define the objects of our application, fall within the scope of Model.

This is the answer to the question about how to interpret the MVC framework design pattern in iPhone. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report