2011년 5월 23일 월요일

iphone Archiving 을 이용한 저장로딩

//
//  SampleDay6_1ViewController.m
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SampleDay6_1ViewController.h"
#import "FourLines.h"

@implementation SampleDay6_1ViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

NSString *filePath = [self dataFilePath];
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
// field1.text = [array objectAtIndex:0];
// field2.text = [array objectAtIndex:1];
// field3.text = [array objectAtIndex:2];
// field4.text = [array objectAtIndex:3];
//
// [array release];
FourLines *fourLines = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
field1.text = fourLines.field1;
field2.text = fourLines.field2;
field3.text = fourLines.field3;
field4.text = fourLines.field4;
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[field1 release];
field1 = nil;
[field2 release];
field2 = nil;
[field3 release];
field3 = nil;
[field4 release];
field4 = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(NSString *)dataFilePath {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [ paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"file.arc"];
}
-(void)applicationWillResignActive:(NSNotification *)notification
{
// NSMutableArray *array = [[NSMutableArray alloc] init];
//
// [array addObject:field1.text];
// [array addObject:field2.text];
// [array addObject:field3.text];
// [array addObject:field4.text];
// [array writeToFile:[self dataFilePath] atomically:YES];
// [array release];
FourLines *fourLines = [[FourLines alloc] init];
fourLines.field1 = field1.text;
fourLines.field2 = field2.text;
fourLines.field3 = field3.text;
fourLines.field4 = field4.text;
[NSKeyedArchiver archiveRootObject:fourLines toFile:[self dataFilePath]];
}

@end


//
//  FourLines.h
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import

#define kField1Key @"key1"
#define kField2Key @"key2"
#define kField3Key @"key3"
#define kField4Key @"key4"


@interface FourLines : NSObject {
NSString *field1;
NSString *field2;
NSString *field3;
NSString *field4;

}

@property (nonatomic, retain) NSString *field1;
@property (nonatomic, retain) NSString *field2;
@property (nonatomic, retain) NSString *field3;
@property (nonatomic, retain) NSString *field4;


-(void)encodeWithCoder:(NSCoder *)encoder;
-(id)initWithCoder:(NSCoder *)decoder;


@end


//
//  FourLines.m
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FourLines.h"


@implementation FourLines

@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;


- (void) dealloc
{
self.field1 = nil;
self.field2 = nil;
self.field3 = nil;
self.field4 = nil;
[super dealloc];
}


-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:field1 forKey:kField1Key];
[encoder encodeObject:field2 forKey:kField2Key];
[encoder encodeObject:field3 forKey:kField3Key];
[encoder encodeObject:field4 forKey:kField4Key];
}


-(id)initWithCoder:(NSCoder *)decoder {
if( self = [super init] ) {
field1 = [[decoder decodeObjectForKey:kField1Key] retain];
field2 = [[decoder decodeObjectForKey:kField2Key] retain];
field3 = [[decoder decodeObjectForKey:kField3Key] retain];
field4 = [[decoder decodeObjectForKey:kField4Key] retain];
}
return self;
}
@end

iphone property list 로 저장하기

//
//  SampleDay6_1ViewController.m
//  SampleDay6-1
//
//  Created by bit on 11. 5. 23..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SampleDay6_1ViewController.h"

@implementation SampleDay6_1ViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

NSString *filePath = [self dataFilePath];
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath]){
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:3];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[field1 release];
field1 = nil;
[field2 release];
field2 = nil;
[field3 release];
field3 = nil;
[field4 release];
field4 = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(NSString *)dataFilePath {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [ paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"file.data"];
}
-(void)applicationWillResignActive:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}

@end

iphone setting bundle

//  1.   Resource  화일에   setting bundle  resource 를 추가한다.

//   UserDefalut   를 이용해서  setting bundle 에 없는 내용도 사용할수 있다.

//  URL Scheme 을 이용하여  어플리케이션으로 접근한다.
//  Settings URL Scheme  검색을 해서 찾아 본다.



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSLog(@"Name = %@", [userDefault objectForKey:@"name_preference"]);
NSLog(@"Enable = %@", [userDefault objectForKey:@"enabled_preference"]);
NSLog(@"Slider = %@", [userDefault objectForKey:@"slider_preference"]);
[userDefault setObject:@"Foo Bar" forKey:@"name_preference"]; 

// "used" 라는 Key 만들어서 초기값을 체크한다
if([userDefault boolForKey:@"used"] == NO ) {
// 초기값을 넣어준다
[userDefault setObject:@"Press Your Name" forKey:@"name_preference"];
[userDefault setBool:YES forKey:@"name_prefernce"];
}
//동기화 시켜준다
[userDefault synchronize];

}

2011년 5월 20일 금요일

아이폰 테이블뷰 상세보기

//
//  TableView.m
//  SampleDay5-1
//
//  Created by bit on 11. 5. 20..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TableView.h"
#import "PictureView.h"


@implementation TableView



#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
     mContryList = [[NSArray alloc] initWithObjects:@"Morocco", @"Japan", @"Swiss", nil];
mImageList = [[NSArray alloc] initWithObjects:@"Africa_Morocco_1", @"Asia_Japan_1", @"Europe_Swiss_1",nil];
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [mContryList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
//큐를 이용해서 재사용한다
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    
// NSString *imageName = [NSString stringWithFormat:@"%@.jpg", [mContryList objectAtIndex:indexPath.row]];
NSString *thumbName = [NSString stringWithFormat:@"%@_thumbnail.jpg", [mImageList objectAtIndex:indexPath.row]];
[cell.textLabel setText: [mContryList objectAtIndex:indexPath.row]];    
[cell.imageView setImage:[UIImage imageNamed:thumbName]];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    
    PictureView *detailViewController = [[PictureView alloc] initWithNibName:@"PictureView" bundle:nil];
// 해당 코드를 삽입한다
[detailViewController loadView];
NSString *imageName = [NSString stringWithFormat:@"%@.jpg", [mImageList objectAtIndex:indexPath.row]];
[detailViewController.mImageView setImage:[UIImage imageNamed:imageName]];
    [self.navigationController pushViewController:detailViewController animated:YES];

    [detailViewController release];
    
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
[mContryList release];
}


- (void)dealloc {
    [super dealloc];
}


@end

아이폰 테이블뷰 셋팅하기

//
//  SampleDay5ViewController.m
//  SampleDay5
//
//  Created by bit on 11. 5. 20..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SampleDay5ViewController.h"

@implementation SampleDay5ViewController
@synthesize mTableView;
@synthesize mFamilyList;
@synthesize mFamilyList2;



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
// 죽어 버린 코드 
//NSArray *array1 = [[NSArray alloc] arrayWithObjects:@"Jake", @"Tom", @"Jenny", @"Mike", nil];
//self.mFamilyList = array1;
//[array1 release];
//NSArray *array2 = [[NSArray alloc] arrayWithObjects:@"Jake1", @"Tom1", @"Jenny1", @"Mike1", nil];
//self.mFamilyList2 = array2;
//[array2 release];

NSArray *array1 = [NSArray arrayWithObjects:@"Jake", @"Tom", @"Jenny", @"Mike", nil];
    self.mFamilyList = array1;

NSArray *array2 = [NSArray arrayWithObjects:@"Jake1", @"Tom1", @"Jenny1", @"Mike1", nil];
    self.mFamilyList2 = array2;

}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[self.mTableView release];
    [super dealloc];
}

#pragma mark -
#pragma mark Tableview data source 
/*
 필수로 만들어 준다. 
 UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( cell == nil ){
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSString *text = [mFamilyList objectAtIndex:indexPath.row];
[cell.textLabel setText:text];
NSString *text2 = [mFamilyList2 objectAtIndex:indexPath.row];
[cell.detailTextLabel setText:text2];
return cell;
return nil;
}

/*
 필수로 만들어 준다.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mFamilyList count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

@end

UITable View

UITableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { return [myStrings count]; }
Section(Group)􏰎 􏱒􏰘􏰇 􏰅􏰆
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section { return [myStrings count]; }
􏰱 Section􏰄 􏰡􏱓􏰍􏱔 􏰴(Row) 􏱒􏰘􏰇 􏰅�

UITableView DataSource


- 셀에 항목 삽입하기 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:@“MyIdenifier”] autorelease]; } cell.text = [myStrings objectAtIndex:indexPath.row] return cell;



UITableView Delegate
- 셀 선택시 동작 지정 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
}
// Get the row and the object it represents NSUInteger row = indexPath.row id objectToDisplay = [myObjects objectAtIndex:row];
// Create a new view controller and pass it along MyViewController *myViewController = ...; myViewController.object = objectToDisplay;
[self.navigationController pushViewController:myViewController animated:YES];