Basic iPhone development tutorial notes

Source: Internet
Author: User

1. Virtual Memory
IPhone OS does not write volatile memory (such as application data) to swap files, so the amount of memory available for applications will be more limited.

Cocoa Touch provides a built-in mechanism to notify applications of insufficient memory.

2. nib file structure
File's Owner is the first icon in all nib files. It indicates the object for loading nib files from the disk. That is, the File's Owner is the object that "owns" The nib File.

First Responder can be understood as the object with which the user is currently interacting. For example, if the user is currently inputting data in textfield, The textfield is the current First Responder.

First Responder will change with the interaction between the user and the interface. Its IB attribute is placeholders, which means that it belongs to a virtual instance and is not as good as the string placeholders of textfield.

It is only temporarily displayed. The real First Responder will be replaced by other objects. In fact, any object derived from the NSResponder class can be used as the First Responder. First Responder
All the actions in them are the response functions provided by or customized by NSResponder.
MacOS maintains a linked list called The Responder Chain in The system. The list contains Responder object instances that respond to various system events. The top object
It is called First Responder, which is the First object to receive system events. If the object does not process the modification event, the system will pass the event down until the object responding to the event is found.
The event is intercepted by the object.
Shows The basic structure of The Responder Chain:

 

 
 


3. Thread Security
Mutable container is not thread safe

"Do not update your UI in the background thread ". In fact, this statement is not strict. First, we need to describe the word "UI Update". It can be understood at two levels:

The first is drawing, which is actually display. Plotting can be carried out in any thread, but to display it, it must be operated in the main thread. For example, add a color filter to an image,

This process can be viewed as plotting. The Twitter client will display a microblog as a cell, but the speed is very fast, because the cell is rendered offscreen first and then displayed.

4. Subclass and Category
Let's talk about the main differences between the two features. In this simple way, subclass reflects the upper and lower-level relationships of classes, while category is a level-1 Relationship between classes.

Category methods shocould not override existing methods (class or instance ).
Two different categories implementing the same method results in undefined behavior
Category not only can add methods to the original class, but if the category method has the same method signature as a method in the class, the method in the category will replace the original method of the class.
This is the replacement feature of category. With this feature, category can also be used to fix some buckets. For example, if a released Framework has a vulnerability and it is difficult to re-release the new version,
You can use the category replacement feature to fix vulnerabilities. In addition, because category has a run-time level integration, the security of the cocoa program is reduced.
Many hackers use this feature (and posting Technology 2) to hijack functions, crack software, or add new features to the software.

5. drawing Issues
As we all know, MacOS is a system that pays great attention to the UI. Therefore, rendering in MacOS programming is a very important part. In part 2, I will introduce the draw programming under MacOS from 2. The first is technical classification, and the second is code structure.
From the perspective of plotting technology classification, Cocoa programmers can access the following several plotting technologies:
1. Cocoa Drawing (NS-prefix)
2. Core Graphics (CG-prefix, called Quazrtz 2D)
3. Core Animation
4. Core Image
5. OpenGL
Here I am not going to show you how each image is drawn. I just want to explain what they look like and what advantages and restrictions they have.
### Cocoa Drawing
Cocoa Drawing should be the first Drawing technology to learn how to develop Cocoa programs. It is also the rendering technology used by most MacOS programs. The underlying layer uses Quazrtz 2D (Core Graphics ). The apple documentation is [Cocoa Drawing Guide] (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html ). Cocoa Drawing does not have a uniform Drawing function. All the Drawing functions are scattered under several main NS classes. For example, NSImage, NSBezierPath, NSString, NSAttributedString, NSColor, NSShadow, NSGradient...
So it's very easy. When you see the following code, you can judge that the Cocoa Drawing method is used.
Copy the Code [anImage drawInRect: rect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
[@ "Some text" drawAtPoint: NSZeroPoint withAttributes: attrs];
NSBezierPath * p = [NSBezierPath bezierPathWithRect: rect];
[[NSColor redColor] set];
[P fill];
 
 
This type of code is exceeded in the drawRect function of NSView. The rendering context of Cocoa Drawing is NSGraphicsContext. I keep seeing many new users confuse NSGraphicsContext with CGContextRef of CoreGraphics. Although they are similar and related, if you do not understand the rendering context, you will often get a blank page result.
### Core Graphics
Core Graphics is the underlying technology of the Cocoa Drawing layer, which is very common in iOS development. Because there is no Cocoa layer in the iOS system, you can find the Core Graphics draw code section on the Internet, this causes a lot of trouble for new users who do not know about Mac development. Cocoa is the application framework under Mac OS, while the application framework under iOS is UIKit. framework, also known as Cocoa Touch. They share some of the Code basics but are not exactly the same. For example, the rendering context of UIView under Cocoa Touch is obtained using UIGraphicsGetCurrentContext (). What it obtains is a CGContextRef pointer, and [NSGraphicsContext currentContext] is used in NSView to obtain the rendering context. It gets an NSGraphicsContext object. Of course, you can use CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort]; In NSView to obtain a Core Graphics rendering context. It can be seen that the development in Mac OS is more flexible. Since the early stage of iOS's UIKit development aimed at video card hardware acceleration, all uiviews are layer-backed by default. IOS developers must use Core Graphics and Core Animation, which are relatively low-layer rendering technologies.
See the following equivalent code to draw a white rectangle. However, Core Graphics and Cocoa Drawing are used respectively:
Copy the code const CGFloat white [] = {1.0, 1.0, 1.0, 1.0 };
CGContextSetFillColor (cgContextRef, white );
CGContextSetBlendMode (cgContextRef, kCGBlendModeNormal );
CGContextFillRect (cgContextRef, CGRectMake (0, 0, width, height ));
[[NSColor whiteColor] set];
NSRectFillUsingOperation (NSMakeRect (0, 0, width, height), NSCompositeSourceOver );
 
It can be seen that this is two painting techniques with completely different styles. Cocoa Drawing is a distributed Drawing function, while Core Graphics is a traditional integrated Drawing method similar to OpenGL. In fact, the lower layer of Cocoa Drawing is Core Graphics, and the lower layer of Core Graphics is OpenGL.
### Core Animation
If Core Graphics and Cocoa Drawing are general UI rendering frameworks, CA is obviously an advanced technology for interface animation painting.
The Cocoa Animation part corresponding to Core Animation should be NSAnimation and NSViewAnimation, but the gap between the two is large. NSAnimation appears after OS X 10.4 and Core Animation is 10.5. NSViewAnimation is relatively simple to use.
To put it simply, Core Animation is used by CALayer and NSAnimation is used by NSView.
 

6. MVC
The MVC model divides all functions into three different categories:
Model: class for saving Application Data

View: elements that can be viewed and interacted with by windows, controls, and other users

Controller: binds the model and view together to determine the graph and process the application logic of user input.

Any written object should be clearly divided into one class, and most of its functions do not belong to or completely do not belong to the other two classes.
MVC can help ensure maximum reusability.


7. Automatic Rotating Screen
For different device screen statuses, two uiviews are designed to be associated with the same xib file, and the output ports are created in ViewController,

In this way, when the screen is changed, the hidden attribute of different views is directly set to avoid adjusting the control position.


8. multi-view
Multi-view applications use the same basic mode.

Common examples include UINavigationController or UITabBarController, and custom subclass of UIViewController.

Note that the multi-view controller is also a view controller. Both Navigation and TabBar are subclasses of UIViewController and can execute all the work that other view controllers can perform.

The root controller is the main view controller of the application and specifies whether the view should be automatically rotated to the new direction.


Content View analysis:

Each View Controller (including multiple view controllers) controls a content view, and the user interface of the application is built in these content views.

Each content view consists of two or three parts: View Controller, nib file, and an optional UIView subclass.

 

9. NSBundle
NSBundle is only a specific file type, and the content follows a specific structure. Both applications and frameworks are bundled with NSBundle.

NSBundle is mainly used to obtain Resources added to the Resources folder of the project. When building an application, these files will be copied to the application bundle.

For example, use mainBundle to obtain the required resource path:

NSString * plistPath = [[NSBundle mainBundle] pathForResource: @ "statedictionary", ofType: @ "plist"];

NSDictionary * dictionary = [[NSDictionary alloc] initWithContentsOfFile: plistPath];

 

10. preferences
A part of the application preferences set by default, which is implemented by the NSUserDefault class for saving and obtaining preferences.

Preferences are stored in the Library/Preferences folder.

 

11. Archive Model Objects
In the Cocoa world, the term "Archive" refers to another form of serialization, but it is a more common type that can be implemented by any object. Compile any object used to save data (model object ).

Supports archiving. As long as each attribute implemented in a class is a scalar (such as int or float) or an instance of a class that complies with the NSCoding protocol, you can archive your object completely.

Because most of the Foundation and Cocoa Touch classes that support data storage comply with NSCoding, it is easier to implement.

Although there are no strict requirements on the use of archive, another protocol, namely the NSCopying protocol, should be implemented with NSCoding, which allows copying objects.

12. Basic data persistence
Method: preference, file, archive, SQLite

 

13. Use Quartz and OpenGL for plotting
We can rely on two different libraries to meet our drawing needs: Quartz 2D, part of the Core Craphics framework, OpenGL ES, which is a cross-platform graphics library.
Although Quartz and OpenGL have many commonalities, there are significant differences between them.

Quartz is a set of functions, data types, and objects. It is designed to draw views or images directly in memory.

Although the RGB model is most commonly used in computer graphics, it is not the only color model. Other models are also used, including:

Color Tone, saturation, value (HSV)

Tone, saturation, brightness (HSL)

Blue-green, magenta, yellow, black (CMYK)

Try to avoid re-drawing the entire view. Instead, call setNeedsDisplayInRect as needed to perform partial re-painting. Reduces the workload of re-drawing a view, and improves application performance.
There are huge differences, especially when applications become more complex.

14. Click, touch, and gesture
Gesture refers to many events that occur when you touch the screen with one or more fingers until your fingers exit the screen. No matter how long it calls, one or

When multiple fingers are still on the screen, you are still in a certain gesture (unless the gesture is interrupted by system events such as incoming phone calls ).
It is important to remember that the iPhone only uses one finger for a tap. If she detects more than one touch, it will reset the tap count to 1.

Responder chain

Because the gesture is passed to the system within the time, the event is then passed to the responder chain. If the first responder does not handle a special event, she will pass the change time to the next level of the responder chain.

15. Core Location positioning
The iPhone can use the Core Location framework to determine its physical Location. Core Location can be implemented using three technologies:

GPS, cell Base Station triangular positioning, and wifi positioning services.

GPS is the most accurate of the three techniques, but not available on the first-generation iPhone. We only need to pass in the precision, and do not need to specify the method. The system determines.

Cocoa Touch uses CLLocationManager.

Remember that the higher the precision you require, the more power you consume. You cannot guarantee that you will obtain the required precision level.
By specifying the distance filter, you can tell the location manager not to notify its proxy of every change. Only when the location change exceeds the specified quantity, the location manager notifies its proxy.

If you only need to determine the current location rather than the continuous polling location, stop the location manager after it obtains the information required by the application.

The more precise the location, the more power is needed.

 

16. Accelerator
By perceiving the total amount of inertial force in a specific direction, the accelerator can measure the acceleration and gravity.

The accelerator in the iPhone is a 3-axis accelerator, which means it can detect motion or gravity in 3D space. The class used is UIAccelerometer.

17. Application Localization

 

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.