2011年2月28日月曜日

debugビルド以外NSLogを出さない

プロジェクトのOther Sourcesフォルダ内のアプリ名_Prefix.pchに以下を追加。
これを使うにはプリプロセッサにDEBUGを追加する必要あり詳しくはこっち
// Debbugログ出力切り替え
#ifdef DEBUG
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...) {}
#endif

NSNotificationの登録

//登録(didloadなどで)
[[NSNotificationCenter defaultCenter] addObserver:class 
selector:@selector(yobimethod:) name:@"notificationName" object:nil];

/*class内*/

-(void)yobu{//呼び出し※引数はなくてもOK 
   NSDictionary *userInfo = 
   [NSDictionary dictionaryWithObject:obj forKey:@"objkey"];
   
   [[NSNotificationCenter defaultCenter] postNotificationName:
        @"notificationName" object:nil userInfo:userInfo];
}


//呼び出されるメソッド
-(void)yobimethod:(NSNotification *)notification{
  NSString*key = [[notification userInfo] objectForKey:@"objkey"];
}

2011年2月24日木曜日

角丸UIView

#import <QuartzCore/QuartzCore.h>//使用クラスにインポートの必要あり

-(void)createKadomaru{
 UIView * maru = [[[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)] autorelease];
 maru.center = self.view.center;
 maru.backgroundColor = [UIColor whiteColor];
 maru.layer.cornerRadius = 4.0;//角の丸み 値が大きいほど丸い
 maru.clipsToBounds = YES;
 [self.view addSubview:maru];
}

←完成はこんな感じ。
今まで角丸部分は画像を用意してたのに…
これは便利。QuartzCoreのインポートを忘れないこと。
clipsToBoundsはUIViewのプロパティでYESならサブビューを
メインビューの内側に描画。(デフォルトはNO)

2011年2月22日火曜日

releaseとdebugで処理を変える

*前準備
Xcode上の左グループとファイル内の最上位にあるプロジェクト名のファイルをクリック
→情報→ビルドタブ→GCC 4.2 - プリプロセス項目内のプリプロセッサマクロに
DEBUGと追加

クラス(どれでもいい)に下記を書くことで処理を分けられる
#ifdef DEBUG
NSLog(@"debugはこっちを通る");
#else
NSLog(@"release,distributionはこっちを通る");
#endif