ラベル Snippet の投稿を表示しています。 すべての投稿を表示
ラベル Snippet の投稿を表示しています。 すべての投稿を表示

9/26/2011

iOSデバイスを判定するSnippet

主にiPadなのかそれ以外なのかを判定するのに使っている。


BOOL isPad = [[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad;
if (isPad) {
} else {
}

読み込むnibファイルを切り替えたり
NSString *nibName = [[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad ? @"viewController_iPad" : @"viewController_iPhone";
UIViewController *viewController = [[UIViewController alloc]initWithNibName:nibName bundle:nil];

9/12/2011

UIImageを指定サイズにリサイズするSnippet


- (UIImage *)resizeImage:(UIImage *)image width:(CGFloat)width height:(CGFloat)height{
UIImage *returnImage = nil; //リサイズ後のUIImage
UIGraphicsBeginImageContext(CGSizeMake(width, height));
[image drawInRect:CGRectMake(0, 0, width, height)];
returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImage;
}

iOSのバージョン判定のSnippet

iOSのバージョンによって処理を変えたい時などに使うSnippet

NSString *versions = [[UIDevice currentDevice]systemVersion];
if ([versions compare:@"5.0"] != NSOrderedAscending) {
} else {
}

上の例はiOS5.0以上なら正とする場合の実装例。

NSLog()文を除去するSnippet

${PRODUCT_NAME}-Prefix.pch内に次のコードを挿入する。
不要時には/* */で囲んでコメントアウトしてやる。
デバッグ時にも使うときと使わない時があるのでこの方法を採用している。

//NSLog()文を除去
#ifndef NSLog
#define NSLog(m, args...)
#endif

NSNotificationCenterのサンプルSnippet

登録、通知、監視、登録解除の流れをごく簡単に列挙。

登録
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(valueChanged:) name:@"ValueChanged" object:nil];

通知
NSString *value = @"Value";
NSNotification *n = [NSNotification notificationWithName:@"ValueChanged" object:value];
[[NSNotificationCenter defaultCenter]postNotification:n];

監視
※事前に登録をしておいた上でselectorのメソッドを実装する。
- (void)valueChanged:(NSNotification *)n{
NSString *str = n.object;
NSLog(@"%@",str);
}

登録削除
- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [super viewDidUnload];
}

NSUserDefaultsのサンプルSnippet

設定画面などでお世話になるNSUserDefaultsの使い方メモ