'Beginning'에 해당되는 글 1건

  1. 2010.06.24 Beginning iPhone Development: Exploring the iPhone SDK
아이폰어플개발정보2010. 6. 24. 14:13
Beginning iPhone Development: Exploring the iPhone SDK

CH2. Appeasing the Tiki Gods

In each Nib file (File’s Owner and First Responder), every icon in this window represents a single instance of an  Objective‑ C class that will be created automatically for you when this nib file is loaded. 

File’s Owner: represents the object that loaded the nib file from disk

First Responder: the object with which the user is currently interacting. The first responder changes as the user interacts with the interface.

AppKit (Mac OS X) --- UIKit (iPhone OS)

CH3. Handling Basic Interaction

MVC model - Any object you write should be readily identifiable as belonging in one of the three categories for maximum reusability.

outlet: (IBOutlet) a pointer that points to an object within the nib

IBOutlet keyword in the header files allows IB to make connections from your code to the nib.

action: (IBAction) part of controller class. 

class methods: factory methods, returns an autoreleased object

“if you didn’t allocate it or retain it, don’t release it”

UIViewController --- corresponding nib file

MainWindow.xib --- in Info.plist -- ApplicationDelegate

Your controller class is the file’s owner for the nib file of the same name.

** connecting outlets **

By  control‑ dragging from File’s Owner to an interface object, you are telling Interface Builder that you want to connect one of the File’s Owner’s outlets to this object when the nib file is loaded.

** specifying actions **

The only thing left to do is to identify which actions these buttons trigger and under what circumstances they trigger them.

 Up Inside.  In most of your iPhone applications if you touch the screen and change your mind. You move your finger off the button before lifting up, right? We should give our users the same ability. If our user’s finger is still on the button when it’s lifted off the screen, then we can safely assume that the button tap is intended.

Ch4. More User Interface Fun

* Controls can be used in many ways.

- active: button, ...

- static: label, ...

- passive: e.g., text field, ...

The way to access the data held by a passive control is to use an outlet.

If you want to display the same image at multiple sizes, generally it’s better to have multiple copies of the image at different sizes in your project rather than force the iPhone to do scaling at runtime.

Tags provide an easy,  language-independent way of identifying objects on your interface. The system will never set or change its value.

Opaque: nothing below this view ever needs to be drawn no matter what.

Clip Subviews: is off by default for the sake of performance

When the user taps the Done button, a “did end on exit” event will be generated.

First responder is the control that the user is currently interacting with. When a text field yields first responder status, the keyboard associated with it goes away.

action sheet - <UIActionSheetDelegate>

All action sheets should have a cancel button.

In situations where you want to notify the user without giving a choice of options, an alert sheet (alert view?) is more appropriate.

Action sheets always have a parent, which must be a view that is currently visible to the user. .

* 4 states of a control

- normal: rest of the other three

- highlighted: when it is being used

- disabled

- selected

Ch5. Autorotation and Autosizing

* tackling autorotation

- autosizing

- manually reposition each object

- different view versions for each rotation

* 4 possible roation

- UIInterfaceOrientationPortrait 

- UIInterfaceOrientationPortraitUpsideDown 

- UIInterfaceOrientationLandscapeLeft 

- UIInterfaceOrientationLandscapeRight 

Because every view controller subclass can implement this differently, it is  possible for one application to support autorotation with some of its views but not with others.

Transformations are mathematical descriptions of changes to an object’s size, position, or angle.

Ch6. Multiview Applications

UITabBarController and UINavigationController are subclasses of UIViewController and can do anything other view controllers can do.

A content view is created indirectly by instantiating its controller class and specifying a nib name. Nibs are typically named using the same name as the controller class.

The root controller gets created when the application loads MainWindow.xib.

The window is the only gateway to the user, so anything that needs to be displayed to the user has to get added as a window subview.

Lazy Loading: do not load resources in the viewDidLoad: method until they are actually needed. ** Loading resources on-demand allows us to freely deallocating those resources when low memory situation occurs. **

** When we changed the underlying class of the file’s owner, the existing outlet connections were broken.  **

**Animation Example**

[UIView beginAnimations:@"View Flip" context:nil]; 

[UIView setAnimationDuration:1.25]; 

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

[UIView setAnimationTransition: transition forView:self.view cache:YES]; 

[coming viewWillAppear:YES]; //#1

[going viewWillDisappear:YES]; //#1

[going.view removeFromSuperview]; 

[self.view insertSubview: coming.view atIndex:0]; 

[going viewDidDisappear:YES]; //#1

[coming viewDidAppear:YES]; //#1

[UIView commitAnimations]; 

#1: Views that have running animations on them, for example, will often choose to turn those animations off when their view is swapped out and turn them back on when they are swapped back in.

Ch7. Tab Bars and Pickers

The picker asks the delegate for either a string or a view that will be drawn at a given spot on a given component.

Picker datasource methods are used by the picker to get the number of components and the number of rows in each component. The datasource isn’t usually an object designed to hold data; it’s an object that supplies data to the picker.

Tab Bar icons - 24x24 pixels in .png format

* Using Interface Builder

1. Declare var.s in Header file

2. Draw controls and set up the classes and properties

3. Implement the Method file

We use NSInteger rather than int or long, because when we use NSInteger, the compiler automatically chooses whichever size is best for the platform for which we are compiling.

Putting in #pragma mark directives and logically organizing your code will make that  pop- up more efficient to use.

bundle is just a special type of folder whose contents follow a specific structure. Applications and frameworks are both bundles.

** fonts on iPhone **

American TypewriterAppleGothicArialArial Rounded MT BoldArial Unicode MS,CourierCourier New, DB LCD Temp, Georgia, Helvetica, Helvetica NeueHiragino Kaku Gothic ProN W3Hiragino Kaku Gothic ProN W6Marker FeltSTHeiti JSTHeiti KSTHeiti SCSTHeiti TCTimes New RomanTrebuchet MSVerdanaZapfino

** convenience class methods use the autorelease pool.

NSString *path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"]; 

SystemSoundID soundID; 

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID); 


Ch8. Introduction to Table Views

A table view is the object that displays the visible part of a table, and a table view cell is responsible for displaying a single row of the table.

Table views get their configuration data from an object that conforms to the UITableViewDelegate protocol and their row data from an object that conforms to the UITableViewDataSource protocol.

* 2 methods that implement the UITableViewCell

- add subviews to UITableViewCell

- subclass UITableViewCell

* 2 types of table view

- grouped table

- indexed (with or without index)

section: group in a table

Mutable copy: shallow copy

It is not safe to remove objects from a collection while iterating that collection.

It’s very important to avoid using convenience methods inside of loops as much as possible, because they will put something into the autorelease pool every time through the loop. However, the autorelease pool can’t get flushed until we’re all done with our loop.

Ch9. Navigation Controllers and Table Views

UINavigationController is implemented as a stack

disclosure indicator vs. detail disclosure control

If tapping a row takes you to another view entirely, one that is not a more detailed view of that row, use a disclosure indicator (gray arrow) to mark the row. 

** 6 styles of cell **

+ Detail Disclosure button

// showing disclosure button

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 

    return UITableViewCellAccessoryDetailDisclosureButton; 


// responding disclosure detail button

- (void)tableView:(UITableView *)tableView 

accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 

+ Checked

newCell.accessoryType = UITableViewCellAccessoryCheckmark;

+ Adding Controls

// add control to the accessory pane

UISwitch *switchControl = [[UISwitch alloc] init];

switchControl.tag = kSwitchTag;

cell.accessoryView = switchControl;

// read value

UISwitch *switchControl = [cell viewWithTag:kSwitchTag];

+ Editing mode

setEditing:animated: method on the table view.

reordering for cell

cell.showsReorderControl = YES;

+ Delete mode

The delete and reorder operations do play nicely together.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

We can’t store raw datatypes like int in an NSDictionary.

Ch10. Application Settings and User Defaults

NSUserDefaults class

There can only be one root node in any given property  list, and all nodes must come under it.

Although you can use any kind of object as a key in an NSDictionary, keys in property list dictionary nodes have to be strings, though you are free to use any node type for the values.

Sliders allow placement of a small  21- pixel   21- pixel image at each end of the slider.

* Type *








NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

objectForKey:, intForKey:, floatForKey: boolForKey:

Ch11. Basic Data Persistence

NSSearchPathForDirectoriesInDomain function

* Document directory *

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0];

// each application has only one Documents directory.

NSString *filename = [documentsDirectory  stringByAppendingPathComponent:@"/theFile.txt"];

* Temporary directory *

NSString *tp = NSTemporaryDirectory();

The downside of using a single file is that you have to load all of your application’s data into memory, and you have to write all of it to the file system for even the smallest changes.

** Property list serialization

Although any object can be made serializable, only certain objects can be placed into a collection class, such as an NSDictionary or NSArray, and then stored to a property list using the collection classes’ writeToFile:atomically: method. 

- NSArray

- NSMutableArray

- NSDictionary

- NSMutableDictionary

- NSData

- NSMutableData

- NSString

- NSMutableString

- NSNumber

- NSDate

[myArray writeToFile:@"/some/file/location/output.plist" atomically:YES];

** Archiving model object

* NSCoding protocol *

You can encode and decode both objects and scalars using  key- value coding.

- (void)encodeWithCoder:(NSCoder *)encoder

- (id)initWithCoder:(NSCoder *)decoder

// if your superclass doesn't conform to NSCoding

   if (self = [super init])

// if your superclass conforms to NSCoding

   if (self = [super initWithCoder:decoder])

* NSCopying protocol *

- (id)copyWithZone:(NSZone *)zone


    MyClass *copy = [[[self class] allocWithZone: zone] init];

    copy.foo = [self.foo copy];


    return copy;// caller releases copy


* Archiving Data object *

NSMutableData *data = [[NSMutableData alloc] init];

NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

[archiver encodeObject:myObject forKey:@"keyValueString"];

[archiver finishEncoding];

BOOL success = [data writeToFile:@"/path/to/archive" atomically:YES];

[archiver release];

[data release];

* Unarchiving Data object *

NSData *data = [[NSData alloc] initWithContentsOfFile:path];

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

self.object = [unarchiver decodeObjectForKey:@"keyValueString"]; // auto-released

[unarchiver finishDecoding];

[unarchiver release];

[data release];

** SQLite3

 object- relational mapping (ORM) is provided by Core Data

// open

sqlite3 *database;

int result = sqlite3_open("/path/to/database/file", &database);

// close


// execution

char * errorMsg;



int result = sqlite3_exec (database, createSQL, NULL, NULL, &errorMsg);

// retrieval


sqlite3_stmt *statement;

int result = sqlite3_prepare_v2( database, [query UTF8String],-1, &statement, nil);

while (sqlite3_step(statement) == SQLITE_ROW) {

    int rowNum = sqlite3_column_int(statement, 0);

    char *rowData = (char *)sqlite3_column_text(statement, 1);

    NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];

    // Do something with the data here

    [fieldValue release];



Ch12. Drawing with Quartz and OpenGL

* Quartz 2D: painter's model on a virtual canvas with invisible pen (context)

* OpenGL ES: state machine, virtual window into its 3D world

** Quartz 2D **

Core Graphics’s API is C based.

// retrieves the current context

CGContextRef context = UIGraphicsGetCurrentContext();

CGPoint(x, y)

CGSize(width, height)

CGRect(CGPoint origin, CGSize size)

Color: (Red, Green, Blue) * alpha

[UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f]

Draw images directly into a context:

CGImage or UIImage (ObjC alternative)

when an object instance is loaded from a nib, neither init: or initWithFrame: ever gets called. Instead, initWithCoder: is used.

setNeedsDisplay:  vs. setNeedsDisplayInRect:

We need to redraw not just the rectangle between firstTouch and lastTouch but any part of the screen encompassed by the current drag.

** OpenGLES **


OpenGL ES doesn’t have sprites or images, per se; it has textures. draw a polygon, and then map a texture onto that polygon

[1] you draw in the context.

[2] once all your drawing is done, you render the context into the buffer.

[3] you present your render buffer, which is when the pixels actually get drawn onto the screen.

Ch13. Taps, Touches, and Gestures

160 pixels per inch

gesture is any sequence of events that happens from the time you touch the screen with one or more fingers until you lift your fingers off the screen.

iPhone only keeps track of tapswhen one finger is used.

The first responder is almost always a view or control and gets the first shot at responding to an event.

responder chain: view -> view's view controller -> view's parent view -> view's parent's view controller -> ...

If an object intercepts an event that it doesn’t handle, it needs to pass it along manually, by calling the same method on the next responder.

Every object in touches is a UITouch event that represents one finger touching the screen.

Every UITouch object knows its current position in the view, as well as its previous position in the view.

Ch14. Core Location

CLLocationManager *locationManager = [[CLLocationManager alloc] init];

Our delegate must conform to the CLLocationManagerDelegate protocol.

// self conforms to CLLocationManagerDelegate protocol

locationManager.delegate = self;

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

locationManager.distanceFilter = 1000.0f;

// start continuously polling the location

[locationManager startUpdatingLocation];

The accuracy value is in meters, so if you specify a desiredAccuracy of 10, you’re telling Core Location that you want it to try to determine the current location within 10 meters, if possible.

Distance filters are also set in meters.

Ch15. Accelerometer

(+x: right, +y: up, +z: front)

Accelerometers give measurements in  g- forces (“g” for gravity), so a value of 1.0 returned by the accelerometer means that 1 g is sensed in a particular direction.

 // singleton

UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];

accelerometer.delegate = self;          // UIAccelerometerDelegate protocol

accelerometer.updateInterval =  1.0f/60.0f;

accelerometer:didAccelerate:           // delegate method

UIAcceleration object                      // passed object for acceleration data

** detect shaking (1.5g ~ 2g, max: 2.3g)

- (void)accelerometer:(UIAccelerometer *)accelerometer 

    didAccelerate:(UIAcceleration *)acceleration {


    if (fabsf(acceleration.x) > 2.0  

        || fabsf(acceleration.y) > 2.0 

        || fabsf(acceleration.z) > 2.0) {

        // Do something here...



If you’re doing animation based on input from the accelerometer, you have to keep track of the time that passes between delegate method calls.

When using the accelerometer as a controller, you’ll need to poll at a considerably faster rate, usually between 30 and 60 updates per second.

Ch16. Camera and Photo Library


1. create an instance of this class

2. specify a delegate

3. specify its image source

4. launch it modally

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;                // UIImagePickerControllerDelegate protocol

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentModalViewController:picker animated:YES];

[picker release];

UIImagePickerController is a subclass of UINavigationController, we have to conform our class to both of these protocols.

Ch17. Application Localization

<2 or 3 language code (lower case)>_<2 letter country code (upper case)>.lproj

Full name localization project, such as French.lprj. exists to support legacy Mac OS X applications, and you should avoid it.

* resouce searching order

1. language_country matching resource

2. language matching resource

3. base language resource

* string files *

/* comments */ 

"First Name" = "First Name"; 


NSString *myString = @"First Name";

NSString *myString = NSLocalizedString(@"First Name", 

    @"Used to ask the user his/her first name");

genstrings ./Classes/*.m

import (not copy) Localizable.strings as UTF-16 char. set file.

출처 : http://blog.naver.com/PostView.nhn?blogId=gonagi&logNo=150051830428
Posted by 오늘마감

댓글을 달아 주세요