Dimly's Lab

iPhone アプリ開発のメモとか。

RSS     Archives
 

スポンサーサイト

 
上記の広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書く事で広告が消せます。
 

TabBar + NavigationBar の組合わせ

 
今回は、TabBar と NavigationBar の組合わせに付いてメモ。

NavigationBar と TabBar それぞれのテンプレはあるけど、
この組合わせのテンプレは存在しない。

しかし、iPhone デフォルトアプリである iPod はこの構造になっている。
こんな構造を実現したいってんで、いろいろやってみた。

プログラミングガイドにもあるように、大前提は「TabBar に NavigationBar を組込む」ということ。

そんでは、れっつとらい。


1. 新規プロジェクト作成

  ファイル > 新規プロジェクト > iOS App > Tab Bar Application
  プロジェクト名は Sample_03 にしました。

2. MainWindow.xib の編集

  Xcode の "グループとファイル" より、Resources > MainWindow.xib をダブルクリック
  IB (Interface Builder) を使って編集して行きます。

  Tab Bar Controller へ、ライブラリから NavigationController をドラッグ&ドロップ。
  これだけで、NavigationController が追加されちゃいます。
  なんともあっさりしてらっしゃる。
  
  追加後の、Tab Bar Controller の中身は
   ・ FirstViewController(First)
   ・ ViewController(Second)
   ・ NavigationController
  ができました。


基本的な追加はこんな感じで終了。
後は、煮るなり、焼くなり、揚げるなり、蒸すなり

図解とかしたらもっとわかりやすいんだろうけど、
あくまでも個人用の備忘録なので、この程度で。


開発メモ
  ■ Project名 : Sample_03
  ■ TabBar と NavigationBar のコラボ。
 

リハビリを兼ねて、テストプログラム作成(2)

 
リハビリを兼ねた、プログラム作成 第2弾

今回のお題は「画面の遷移」

れっつ ぷろぐらむ。


1. 新規プロジェクト作成。

  ファイル > 新規プロジェクト > iOS App > Navigation-based Application
  プロジェクト名は Sample_02 とした。


2. 新しい画面(View)を追加

  ファイル > 新規ファイル > iOS App > Cocoa Touch Class > UIViewController subclass
  オプションは
    - UITableviewController subclaa
    - With XIB for user interface
  にチェックして次へ

  名前は AnotherViewController とした。
  オプションとして
    - 同時に"ファイル名.h" も作成
  にチェックして完了

3. RootViewController.m の編集

  RootViewController.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
  ここでは、セクションの数を指定する。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
  ここでは、セクション毎の行数を指定する。
  今回は1セクションなので、このままでいいけど、
   - 複数セクションになった場合
   - 各セクションで行数が異なる場合
  こんな時は、 Switch 文を使えば解決できる。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch( section ) {
case 0: return 1;
case 1: return 3;
case 2: return 5;
}
}
  この例では、
   セクション1は1行
   セクション2は3行
   セクション3は5行
  となる。

4. 画面遷移アクション

  今回は、「TableView の Cell をタップした時に、画面遷移する」という動作を考える。

  アクション自体は、RootViewController 内にある。


  RootViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];

[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
}
  ここでは Cell が選択されたときの初期を記述している。
  今回は、画面遷移を行うので、別の画面を呼び出している。

AnotherViewController *anotherViewController =
[[AnotherViewController alloc]
initWithNibName:@"AnotherViewController" bundle:nil];
  ここに注目。
  ここで、先ほど作成した AnotherViewController を呼び出しています。

  最後に、RootViewController.m で、AnotherViewController.h をインポートする

#import "RootViewController.h"
#import "AnotherViewController.h"
  以上。
  これで、Table の Cell をタップすると、画面遷移が出来る様になります。

  ちなみに、RootViewController に戻るには、NavigationBar に
  自動的に作成されているボタンをタップする事で戻れます。


開発メモ
  ■ Project名 : Sample_02
  ■ UINavigationを使って画面遷移。
 

リハビリを兼ねて、テストプログラム作成

 
最後に objc を触ってたのが半年程前…
ついでに、各自お勉強と言う事でお題が出た。

と、言う事でリハビリ。

今回のお題は「ボタン押下で日付を表示する」というもの。

れっつ ぷろぐらむ。


1. 新規プロジェクト作成。

  ファイル > 新規プロジェクト > iOS App > View-based Application
  プロジェクト名は Sample_01 とした。

2. IBOutlet と IBAction を追加

  Sample_01ViewController.h
#import

@interface Sample_01ViewController : UIViewController {
IBOutlet UILabel *display;
}

- (IBAction) GetDate;

@end


  Sample_01ViewController.m
- (void)UpdateDisplay
{
NSDate *tmpDate = [NSDate date];

//カレンダーを生成する
NSCalendar* cal = [ NSCalendar currentCalendar ];

//取得したいコンポーネントのフラグを生成する
NSUInteger flags = NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;

// コンポーネントを生成する
NSDateComponents *cmp = [cal components:flags fromDate:tmpDate];

// 月、日、曜、時、分、秒 を取得
int monthInt = [cmp month];
int dateInt = [cmp day];
int weekInt = [cmp weekday];
int hourInt = [cmp hour];
int minitueInt = [cmp minute];
int secondInt = [cmp second];

// 曜日の取得
NSMutableArray *weeks = [NSArray arrayWithObjects:@"Sun", @"Mon", @"Tue", @"Wed", @"Thu", @"Fri", @"Sat", nil];
id weekObj = [weeks objectAtIndex:(weekInt-1)];

// 各値をを文字列の変換する
NSString *dateString = [NSString stringWithFormat:@"%02d / %02d (%@) %02d:%02d:%02d"
,monthInt,dateInt,weekObj,hourInt,minitueInt,secondInt];

// 値を出力する
display.text = dateString;
}


// Button押下
- (IBAction)GetDate
{
[self UpdateDisplay];
}


  それぞれこんな感じで書いてみた。
  時間の表示だけなら、

  
// 省略版
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"YYYY/MM/dd (ccc) HH:mm:ss"];

display.text = [dateFormatter stringFromDate:[NSDate date]];


  でも簡単に出来るようだけど、今後、各値を使うかもしれないので、
  少々面倒だけど、各値を変数へ突っ込んでみた。

3. IBOutlet と IBAction の結合

  今回の画面はIB(Interface Builder)を使用。
  ボタンとラベルを配置して、それぞれに IBOutlet と IBAction を結合。

4. Build と 実行

  ここまでできたら、後は実行して動作確認するだけ。
  ボタンを押して、ラベルに日時情報が表示されるのを確認して終了。


開発メモ
  ■ Project名 : Sample_01
  ■ ボタン押下により、日時情報を取得し、表示する。
Profile

Author : Dimly


Calendar
04 | 2012/05 | 06
S M T W T F S
- - 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 - -




Search