Basic iOS learning Design Patterns

Source: Internet
Author: User

IOSLearning basicsDesign ModeThe detailed description is the content to be introduced in this article. It mainly introduces some basic design patterns in iOS. Let's take a look at the content first.






Declared properties

The following sections describe eachDesign Mode.


This is Apple's authoritative explanation.

Delegation: The delegate mode also has a common proxy mode. The difference between the two is that the proxy mode is generally stricter and the same interface is implemented. The delegate only references the delegated object ), is a simple and powerful mode that allows an object to act as another object. The delegate object persists to the reference of another object and sends a message to another object when appropriate. The delegate object can do some extra work when sending messages.

In the delegate mode in the cocoa framework, the delegate object is often an object in the framework, and the delegated object is a custom controller object. The delegated object maintains a weak reference to the delegated object.

The example in this document is developed on mac.

Here, the delegated object is an instance of the NSWindow class, which declares the protocol). One of the methods is windowShouldClose. When you click the close button of the window, window object will send the windowShouldClose message to the proxy object windowDelegate), ask whether to close the window. The proxy object returns a bool value, which controls the behavior of the window object.

There are similar examples in iOS. In the created Project, the classes directory contains xxAppDelegate, which is the delegate class. In this delegate class instance, UIWindow and Controller are referenced. Similar to the mac example above, the behavior of the referenced object can be controlled.


Can be considered as an interface in java ). There are two protocol types in iOS:

Formal protocol: @ interface declaration requires subclass implementation methods, which can be forced to implement @ required) or optional @ optional. The formal protocol is an extension of the objc language;

Informal protocol is a category of NSObject, and category is a feature of the objc language. It allows you to add methods for a class without subclass implementation. The informal protocol implementation method is optional. Before the Optional protocol method does not introduce objc 2.0, the informal protocol is the basic way to implement delegation in the foundation and appkit classes.


You should be familiar with the MVC design pattern.


Target-action is a design pattern. The object maintains necessary information and sends messages to other objects when an event occurs. The maintained information consists of two parts:

Action selector, which defines the name of the method to be called;

Target, the object that receives the message.

When an event called action message occurs, the method message defined by action selector is sent to the target.

The target-action mode is generally used for custom controllers to process action messages in a way defined by application specifications.


A notification is a message used to notify one or more observer objects that an event occurs. Here, the recipient is the observer and observer. It is actually the observer mode.

Here, the recipient of the notification does not know who will receive the notification. In this way, the notification and the observer are loosely coupled.

Declared property

It is a declared attribute. For example:

  1. @property (<#attributes#>) <#type#> <#name#>; 

It is something that enhances the objc syntax and simplifies the "syntactic Sugar" nature of code writing.

Summary:IOSLearning basicsDesign ModeI hope this article will help you!

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: 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.