Ios Development Guide-Chapter 1-3

Source: Internet
Author: User
Tags notification center

Preface:

After learning the underlying c and c ++, we can now go back to the upper-layer interface design. Since we have done this, we must do it well, just as we have done in the past in c and c ++. In the past year, I have carefully studied the mfc, C ++ programming language, objective-c ++, GOF, and big talk design modes, and read more objective c ++, c ++ premier, linux program design, harly guide linux, consciously in the c ++ aspect of the result is not bad, and now re-pick up just half a year of ios, on the one hand, because of money, on the other hand, it is because the underlying capabilities did not work in the past, and the interface was in a state of knowing but unknown. Now, it is necessary to read books again. I bought two books, ios development guide and ios design mode analysis. This blog record the learning process of books, as well as the unfinished tasks in the past. It concentrates good ideas and strategies for my use.

Ios Development Guide

Books written by Chinese people have always been resistant to technical books of Chinese people. However, the directory structure of this book is more suitable for me, so I gave up the book that is proficient in ios development and chose it. This book is a little too thick. It is close to 700 pages. It is not suitable for holding it in your hand. It is too heavy. There are many things to talk about, but it is not complicated. There are some errors and the content is not very advanced, it is suitable for a small half bucket of water like me. There is no time for parsing the ios design pattern. Let's just let it fall asleep and pick it up so that you can get started with the company's code quickly. Otherwise, I will suffer from future work.

Chapter 1

Xcode introduction, book pictures, content Introduction, source code links in the book. There is no significant content.

Chapter 2

The life cycle of an application is worth reading.

Open the program: Not Run (applicationDidFinishLauch)-> InActive (ApplicationDidBecomeActive)-> Active

Application background: Active (applicationWillResignActive)-> InActive-> Backgroud (applicationDidEnterBackgroud)-> Suspend (applciationWillTerminate)-> Terminate

Pending running: Suspend-> Backgroud (applciationWillEnterForgroud)-> InActive (applicationWillBecomeActive)-> Active

When the system memory is insufficient, the Suspend program in the background may be killed by the system without notice.


View Lifecycle

LoadView (get a view)-> ViewDidLoad (add a subview after the generated view for customization)-> ViewWillAppear-> viewWillDisappear-> didReceiveMemoryWarnning (receive a memory warning)-> viewDidUnLoad

LoadView is used to create self. view. When loaded from the nib file, self. view is directly created. Therefore, loadView is not required.


Xcode project and target

A project corresponds to multiple targets. The project settings are public, and the target settings can overwrite the project settings. Under the target summary option, set the screen icon 57*57, icon.png, the screen of the chain player is icon@2x.png, the screen of the chain player is 320*480, and the screen of the chain player is defa@@2x.png. The options are supported by the device.


Ios Api Overview

Too many frameworks are difficult to remember, and the necessity is not great. Note that there are four levels: Cocoa Touch, Media, Core Services, and Core OS. What is contained in each layer, so you do not care about it for the moment. And how to use the help documentation, alt + mouse click-to open the help documentation


Chapter 3 Basic Design Model

I often use ios design patterns. I am good at it and it is much simpler.

Singleton Mode

The synchronized mechanism is not used. The dispatch_once mechanism of gcd is used to maintain only one operation, which is more efficient than locking. As for the internal principle, I do not understand it yet, however, we will deal with its underlying architecture in the future. A simple code example to facilitate future copy

@ Interface Singleton: NSObject

@ End

@ Implementation Singleton

Static Singleton * _ shareSingleton = nil;

+ (Singleton *) sharedManager

{

Static dispatch_once_t once;

Dispatch_once (& once, ^ {

_ ShareSingleton = [[Singleton alloc] init];

// ...... Your other initial code here

}

);

Return _ shareSingleton;

}

The idea is simple: Private Static pointer. The class method allocates memory and returns this pointer. To prevent simultaneous access by multiple threads, several objects may be created, and locks can be used. To improve efficiency, there is a secondary Detection Policy. The GCD content used here is guaranteed to be executed only once.

Example: UIAccelerometer NSUserDefault NSNotificationCenter.


Delegation Mode

One of my more scratching-head models is that I have defined a framework, but want to give users personalized operations, so I extracted personalized operations and made them into a protocol, let the customer implement the Protocol. For example, ViewController implements the UITableViewDelegate protocol. viewController holds a UITableView pointer and sets tableView. delegate = self. In this way, tableView is equivalent to leaving personalized manipulation to ViewController.

Simple class diagram:

UITableViewDelegate

|

|

UITableView (holding the delegate pointer) -------------> MyViewController (holding the tableView pointer and setting delegate)


MyViewController inherits the delegate and holds the tableView pointer at the same time. This is a strong reference. UITableView also holds a delegate pointer. Because the pointer is assigned as a MyViewController object, it must be a weak reference to avoid circular reference. Generally, tableView. delegate is set to itself in MyViewControlller, that is, MyViewController is responsible for implementing the delegate method. In this way, UITableView implements the basic framework of TableView, and makes the user's personalized and scalable parts into a protocol for the customer to implement. The Protocol call must be performed by the UITableView internal framework. when data is required, the protocol needs data. The Protocol provides users with maximum scalability, at the same time, you don't have to worry about how UITableView is presented internally. You only need to provide data. Example UITextField Application


Observer Mode

It is a simple design mode, but it is quite common. Remember one Subject and n multiple observers. Subject needs to implement the addObserver, removeObserver, and notifyAllObservers methods. At the same time, there is an observers array to save all the observers; observer needs to implement the update method. Call the yyallobservers method to update data. This method traverses all the observers of the array and calls the update method of the observer to update data. The notification center is a subject. To make it easy to use in the singleton mode, you need to implement a notification center when you are free, so that you can understand it more deeply.

KVO is also an observer mode. KVO automatically creates a subclass for the Subject class and points the isa pointer of the Subject class to the derived class. the setValueForKeypath method is used to add the code for notifying the observer to a derived class, because the notifyObserver method is added to the derived class. To add a Subject for Attribute observation, you must call the addObserver method to add the observer. When the attribute changes, the observer's ObserverForKeyPath will be called. It is not complicated to implement one at a time, mainly because the internal mechanism is not fully understood by itself.


MVC Mode

The MVC model and view in Cocoa do not have any interaction, and they are all interacted through the man-in-the-middle ViewController.

ViewController holds many View object pointers (IBOutlet), so you can directly manipulate the view. The view can send the operation feedback to ViewController through the delegate and Protocol mechanisms, as well as the callback (IBAction) of the button) you can also report to the Controller.

The Controller also generally holds the model pointer and can directly update the model data. The model data can be notified to the Controller by using the Notification and KVO mechanisms. The above several strategies basically summarize the entire MVC. The Stanford open class has a good picture, which deserves your attention.


OK. Now, you have to go home. It's too late. Continue writing tomorrow.




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.