2010년 5월 25일 화요일

CustomNavigationController

0. 목적
title 영역에 background image 넣고싶다.

1. 구현


@interface CustomNavigationController : UINavigationController {

}

@end


#import "CustomNavigationController.h"


@implementation UINavigationBar(background)

- (void)drawRect:(CGRect)rect {

[super drawRect:rect];

UIImage* image = [UIImage imageNamed: @"title_bg.png"];

[image drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];   

}

@end


@implementation CustomNavigationController

@end

2010년 5월 4일 화요일

2010년 4월 23일 금요일

iphone logger

0. 목적
simply iphone logger

1. 참고
http://stackoverflow.com/questions/202299/logging-to-a-file-on-the-iphone
http://www.iphonedevsdk.com/forum/iphone-sdk-development/8162-debugging-tip-nslog.html

2. 조건
configuration=debug 일때만 로그를 남긴다.

3. 구현

3-1. macro setting
preprocessor macro 에 설정




3-2. Logger.h , m

#ifdef DEBUG

#define Log(s, ...) NSLog(s, ##__VA_ARGS__)

#else

#define Log(s, ...)

#endif


@interface Logger : NSObject {


}

+ (NSString*)path;

+ (void)write:(NSString*)str;

+ (NSString*)read;

+ (void)remove;


@end


#import "Logger.h"


@implementation Logger


+ (NSString*)path{

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *path = [docsDirectory stringByAppendingPathComponent:@"log.txt"];

return path;

}


+ (void)write:(NSString*)str{

#ifdef DEBUG

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"HH.mm.ss"];

NSString* log = [NSString stringWithFormat:@"%@-%@\n\r" , [dateFormatter stringFromDate:[NSDate date]] , str];

[dateFormatter release];

NSString* storeLog = [Logger read] ? [Logger read] : @"";

NSData *dataToWrite = [[NSString stringWithString:[storeLog stringByAppendingString:log]] dataUsingEncoding:NSUTF8StringEncoding];

[dataToWrite writeToFile:[Logger path] atomically:YES];

#endif

}


+ (NSString*)read{

return [[[NSString alloc] initWithContentsOfFile:[Logger path]] autorelease];  

}


+ (void)remove{

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:[Logger path]]){

[fileManager removeItemAtPath:[Logger path] error:NULL];

}

[fileManager release];

}


@end


3-3 delegate 에 추가

#ifdef DEBUG

#if TARGET_IPHONE_SIMULATOR == 0

Log([Logger read]);

[Logger remove];

#endif

#endif




iphone simulator & device 구분하기

#if TARGET_IPHONE_SIMULATOR == 1

NSLog(@"simulator");

#else

NSLog(@"device");

#endif

2010년 4월 19일 월요일

custom UIPageControl

0. 목적 및 동기

요구되는 디자인이 기본 UIPageControl의 UI를 사용하지 않는다.

그래서 CustomPageControl 이 필요해졌다.

UI control구조에 익숙한 사람이라면 control 들은

subcontrol 들의 조합으로 이루어 졌다는 사실을 예상할 수 있을것이다.

UIPageControl 도 마찬가지로 점들을 이루는 UIImageView 가

subcontrol 로 존재한다.


1. 코드


@interface CustomPageControl : UIPageControl {

}


@end


#import "CustomPageControl.h"

#define kOffImage [UIImage imageNamed:@"offImage.png"]

#define kOnImage [UIImage imageNamed:@"onImage.png"]


@implementation CustomPageControl


- (void)layoutSubviews{

[super layoutSubviews];

UIImage* image = kOffImage;

for(UIImageView* view in self.subviews){

CGRect frame = view.frame;

frame.size = image.size;

view.frame = frame;

view.image = image;

}

}


- (void)setCurrentPage:(int)index{

   [super setCurrentPage:index];

for(int i = 0 ; i < [self.subviews count] ; i++){

UIImageView* view = [self.subviews objectAtIndex:i];

view.image = (i == index) ? kOnImage : kOffImage;

}

}


@end


2010년 4월 13일 화요일

popToViewController + NSNotification

0. 목적
UINavigationController의 popToViewController 메소드와 NSNotification 를 이용하여
특정시점의 viewController로 가서 페이지를 이동해보자.
A -> B -> c 순서로 viewController 이동되었다고 가정하고,
B,C를 무시하고 A 에서 D 로 바로 이동한것과
 같은 기능이 필요할때 적합하다.
(중간에 세팅화면 같은것을 거쳐야 할 경우등)

 
1. 코드

1-1. rootViewController

#import "Page2ViewController.h"

#import "Page1ViewController.h"

#import "popToViewControllerViewController.h"


@implementation popToViewControllerViewController

- (void)viewDidUnLoad{

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"goPage2" object:nil];

}


- (void)viewDidLoad{

self.title = @"root";

self.view.backgroundColor = [UIColor yellowColor];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goPage2)    name:@"goPage2" object:nil];

UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(100,100,100,100)];

[button setTitle:@"go page 1" forState:UIControlStateNormal];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[button addTarget:self action:@selector(goPage1) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

[button release];

}


- (void)goPage1{

Page1ViewController* viewController = [[Page1ViewController alloc] init];

[self.navigationController pushViewController:viewController animated:YES];

[viewController release];

}


- (void)goPage2{

[self.navigationController popToViewController:self animated:NO];

Page2ViewController* viewController = [[Page2ViewController alloc] init];

[self.navigationController pushViewController:viewController animated:YES];

[viewController release];

}

@end



1-2. page1ViewController

@implementation Page1ViewController


- (void)viewDidLoad{

self.title = @"page1";

self.view.backgroundColor = [UIColor redColor];

UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(100,100,100,100)];

[button setTitle:@"go page 2" forState:UIControlStateNormal];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[button addTarget:self action:@selector(goPage2) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

[button release];

}


- (void)goPage2{

[[NSNotificationCenter defaultCenter] postNotificationName:@"goPage2" object:nil];

}

@end


1-3. page2ViewController

@implementation Page2ViewController


- (void)viewDidLoad{

self.title = @"page2";

self.view.backgroundColor = [UIColor grayColor];

}


@end



test source :

2010년 3월 28일 일요일

iphone libxml2

0. 목적
iphone에서 xpath 사용해 보자.


1. 참고
1-1. http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html (wrapper)

2. 설정
1-2 링크의 내용처럼 libxml은 OS 기본내장이기때문에 따로 받아서 설치할 필요는 없고,
configuration 세팅만 해주면된다.

 2.1 header search Paths 에 링크추가
  -  /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk/usr/include/libxml2 (device 용)
  - /usr/include/libxml2 (simulator 용)