2012년 6월 22일 금요일

[iOS] UIDevice 클래스를 통한 디바이스 정보 가져오기

UIDevice

UIDevice 클래스는 기기 정보와 관련된 여러 프로퍼티들을 가지고 있다. 사용 빈도에 비해 유난히 기억이 안나 늘 찾아보게 되는 구문이다. 기억 차원에서 포스팅.


UIDevice *device = [UIDevice currentDevice];

// e.g. "My iPhone"
NSLog(@"Device Name : %@", device.name);

// e.g. @"iPhone", @"iPod touch"          
NSLog(@"Device Model: %@", device.model);

// e.g. @"4.0"   
NSLog(@"System Name : %@", device.systemVersion);

// e.g. @"iOS"
NSLog(@"System Version : %@", device.name);

// localized version of model
NSLog(@"Localized Model : %@", device.localizedModel);

참조

[iOS] UITextField를 포함하는 Custom UITableViewCell

Tumblr iOS Application

텀블러 앱의 로그인 화면과 같은 입력 폼을 만들기 위해 UITableViewCell을 상속받아 간단한 커스텀 테이블 뷰 셀을 만들었다. 커스텀 테이블 뷰 셀을 만들기 위해선 다음 세 가지만 기억해두면 될 듯 하다.
  1. UITableViewCell 클래스를 상속하여 원하는 뷰를 추가.
  2. initWithStyle:reuseIdentifier: 메시지에서 추가된 뷰를 초기화.
  3. layoutSubviews 메시지를 오버라이드하고 bringSubviewToFront: 메시지를 추가한 뷰를 전면으로 표시한다.
FieldTableViewCell.h
#import 

@interface FieldTableViewCell : UITableViewCell

@property (nonatomic, retain) UITextField *textField;

@end

FieldTableViewCell.m
#import "FieldTableViewCell.h"

@implementation FieldTableViewCell
@synthesize textField;

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

- (id)initWithStyle:(UITableViewCellStyle)style 
    reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textField = [[UITextField alloc] initWithFrame:CGRectZero];        
        [self.contentView addSubview:self.textField];
    }
    
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.contentView bringSubviewToFront:self.textField];
    
    CGSize labelSize = [self.textLabel.text sizeWithFont:self.textLabel.font];
    
    CGRect frameForTextField;
    frameForTextField.size.width = 
        self.contentView.frame.size.width - labelSize.width - 30.0f;
    frameForTextField.size.height = 31.0f;
    frameForTextField.origin.x = labelSize.width + 15.0f;
    frameForTextField.origin.y = 11.0f;
    
    self.textField.frame = frameForTextField;
}

@end