Archive for category Programming
[iPhone]Debug Log用マクロ
Posted by takatronix in Programming, iPhone on 2009/04/16
xcodeでデバッグログを出力するときは普通NSLog()を使うのだが、これが結構重たい上、Releaseビルドでも出力されてしまうのでどうしたもんかと思っていたのだが、設定とマクロでうまくできるのがわかったのでメモ。
プロジェクトの設定を開く。構成でDebugを選択
左下のボタンを押してユーザー定義の設定を追加
GCC_PREPROCESSOR_DEFINITIONSを追加し、DEBUGを設定する

以下のマクロを、インクルードされるヘッダに定義する
#ifdef DEBUG # define TRACE(fmt, ...) NSLog((@"%s(%d) " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define TRACE(...) #endif
ログを出力したい場所で以下の様に使う
TRACE("startDownload genre=%@",genreId);
出力結果
2009-04-16 18:57:18.519 Beatport[14249:20b] -[TrackListViewController startDownload:](76) startDownload genre=10
「オブジェクト名 メソッド名(行番号) ログ」
という形式で出力される。マクロを以下の様に変更するにすると、ファイル名(行番号)で出力される
# define TRACE(fmt, ...) NSLog((@"%s(%d) " fmt), __FILE__, __LINE__, ##__VA_ARGS__);
出力結果
2009-04-16 19:01:19.699 Beatport[14317:20b] 2009-04-16 19:01:45.633 Beatport[14317:20b] /Users/takatronix/Documents/Beatport/Classes/TrackListViewController.m(76) startDownload genre=10
tAkatronixおすすめのiPhone開発本
ソフトバンククリエイティブ
売り上げランキング: 84906
ソフトバンククリエイティブ
売り上げランキング: 4163
リックテレコム
売り上げランキング: 3089
ソフトバンククリエイティブ
売り上げランキング: 22615
[iPhone]ActionSheetでCancelボタンが効かない問題
Posted by takatronix in Programming, iPhone on 2009/02/22
現在作成中のiPhoneアプリでダウンロード中の状態表示をするようにしてみた。
ActionSheetを作成して、それにUIProgressViewを貼り付けるまでは簡単だった。
オブジェクトをコントローラクラスに追加し、
UIActionSheet *actionSheet; UIProgressView *progressBar; UILabel *progressLabel;
ViewDidLoadでActionSheetを作成
-(void)createActionSheet {
if(!actionSheet) {
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please wait...\n\n\n\n"
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel",nil)
destructiveButtonTitle:nil
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;
}
if(!progressBar) {
progressBar = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 40.0f, 260.0f, 90.0f)];
progressBar.progressViewStyle = UIProgressViewStyleDefault;
progressBar.progress = 0.0f;
[actionSheet addSubview:progressBar];
progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 50.0f, 240.0f, 20.0f)];
progressLabel.backgroundColor = [UIColor clearColor];
progressLabel.textColor = [UIColor whiteColor];
progressLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
progressLabel.text = NSLocalizedString(@"Downloading...", nil);
[actionSheet addSubview:progressLabel];
}
}
ダウンロードの開始のタイミングでActionSheetを作成
- (IBAction)startDownload:(NSString*)genreId {
downloadedContentLength = 0;
progressBar.progress = 0;
progressLabel.text = NSLocalizedString(@"Downloading", nil);
[actionSheet showInView:self.view];
// 以下略
ダウンロードの処理の途中にUIProgressViewを更新してやると完成

ここまではサンプルコード通りにやれば簡単だった。
しかしどうやってもキャンセルボタンが効かない。
ViewDidLoadでActionSheetを表示させるとキャンセルボタンが効くが画面の真ん中に表示されてしまう。
ActionSheetのボタンがキャンセルボタン一つだとうまくいかないが、ボタンを増やすとキャンセルが効く・・・。???
showInViewを使わずに、showFromTabBarを使うと上からアクションシートが降りてくるがキャンセルボタンが効く。
ググると同じような問題がたくさんあって、うまく動かないので別のスレッドを使えととかかいてある。
初期化のタイミングを変えたりスレッド化したり一晩さんざん悩んだあげく、よくよく調べてみるとSDK2.2のバグらしい。
んで、いろいろ調べた結果一番エレガントな回避コードは
こいつを
[actionSheet showInView:self.view];
こうしてやることだった。
[actionSheet showInView:self.view.window];

tAkatronixおすすめのiPhone開発本
ソフトバンククリエイティブ
売り上げランキング: 84906
ソフトバンククリエイティブ
売り上げランキング: 4163
リックテレコム
売り上げランキング: 3089
ソフトバンククリエイティブ
売り上げランキング: 22615
[iPhone]プロパティリストを読み込む NSDictionary
Posted by takatronix in Programming, iPhone on 2009/02/14
UITableViewにbeatportのジャンルのリストを表示するのに一番スマートな方法を模索していていい感じにやる方法がわかったのでメモ。
ベタにコードに書かずにプロパティリストエディタでgenre.plistを作成しリソースに追加する。

テーブルビューに表示する名称の配列とジャンルのデータを定義する
@interface RootViewController : UITableViewController {
NSArray* names;
NSDictionary *dictGenre;
}
@property (retain) NSArray* names;
@property (retain) NSDictionary* dictGenre;
プロパティリストファイルをNSDictionaryクラスに読み込み、キー一覧をソートしてNSArrayオブジェクトへ展開する迄
// Genre.plistの読み込み NSString *pathGenre = [[NSBundle mainBundle] pathForResource:@"Genre" ofType:@"plist"]; // RootViewControllerを取得 RootViewController* rootViewController; rootViewController = (RootViewController*)navigationController.topViewController; rootViewController.title = @"Grenres"; rootViewController.dictGenre = [NSDictionary dictionaryWithContentsOfFile:pathGenre]; // NSDicronayのkeyを配列に取り出してソート NSArray *sortedArray =[[rootViewController.dictGenre allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; rootViewController.names = sortedArray;
UITableViewのセル描写のコールバックでソートしたジャンル名を表示してやる。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [names objectAtIndex:indexPath.row];
return cell;
}
ウホッ

tAkatronixおすすめのiPhone開発本
ソフトバンククリエイティブ
売り上げランキング: 84906
ソフトバンククリエイティブ
売り上げランキング: 4163
リックテレコム
売り上げランキング: 3089
ソフトバンククリエイティブ
売り上げランキング: 22615
[iPhone]UIImageViewにUILableで動く時刻を貼り付ける
Posted by takatronix in FREEEK, Programming, iPhone on 2009/02/10
ぐるぐる動かしているイメージ(UIImageView)に動く時計の文字列を貼り付けるのをどうやって実現するかでなやんだのだが、UIImageViewにUILabelを貼り付けたらどうだろうと思ってやってみたらうまくいった。
InterfaceBuilderだとUIImageViewのSubViewとして貼り付けることができなかったので、このようにUILabel を作成し、UIImageView* bImageのサブビューとして貼り付けた。
UILabel* timeLabel; CGSize szImage = bImage.bounds.size; timeLabel = [[[UILabel alloc] initWithFrame: CGRectMake(5,160,szImage.width,170)] autorelease]; timeLabel.font = [UIFont boldSystemFontOfSize:15.0]; [bImage addSubview:timeLabel]; timeLabel.textColor = [UIColor whiteColor]; // 文字色白 timeLabel.backgroundColor = [UIColor clearColor]; // 背景透明 timeLabel.textAlignment = UITextAlignmentCenter; // センタリング
UIImageを回転させるとUILabelも同じように回転する。
MFCのCWndクラスとは比べものにならないくらい高機能だな。時代の変化を感じるw。

時刻文字列の作成はタイマーのコールバックで以下のようにやった。
NSDate *date = [NSDate date]; // 現在の日付を得る NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY/MM/dd kk:mm:ss"]; NSString* strTime = [dateFormatter stringFromDate:date]; timeLabel.text = strTime;

ぐーるぐるまわったり拡大しても時刻もスムースに同期して動くね。
tAkatronixおすすめのiPhone開発本
ソフトバンククリエイティブ
売り上げランキング: 84906
ソフトバンククリエイティブ
売り上げランキング: 4163
リックテレコム
売り上げランキング: 3089
ソフトバンククリエイティブ
売り上げランキング: 22615







[iPhone]高精度タイマーの使い方 NSTimeInterval