Tabbed Applicationのタブ追加

Tabbed Application

昔はTab Bar Applicationという名前だったぽいけど、
Tabbed Applicationという名前に変わったぽい。

すると、MainView.xibなどのMainView関係のファイルがなくなっていたので、
3つ目のタブを追加できずに困った。

タブの世界観

UITabBarControllerが下のバーと上の表示部分を持ってるイメージ。


そんなイメージなんだけど、
上下の部分は、厳密にはFirstViewControllerやSecondViewControllerクラスとして持ってるイメージ。

UITabBarControllerに3番目のタブを登録する(AppDelegate.mに自分で)

ということで、MainViewがないので、タブが作られているコードをいじる。
該当箇所はAppDelegate.m。

まずタブの上の部分に相当するところをインスタンス

AppDelegate.mを以下みたいな感じに

    UIViewController *viewController1 =
     [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

    UIViewController *viewController2 =
     [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];

    // ここ追加
    UIViewController *viewController3 =
     [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];

要は、自分で作った三つ目のタブ(今回は、MyViewController)をallocして
initWithNibName引数に.xibファイルの名前を与える。

tabBarControllerにインスタンス化したviewController追加

インスタンスを作ったのでこれをtabBarControllerに登録してやる。
AppDelegate.mに該当箇所があるので、そこに引数を足す。

    self.tabBarController.viewControllers =
        [NSArray arrayWithObjects:viewController1,
                                  viewController2,
                                  viewController3,   // ここ追加
                                  nil];
自分で作った三つ目のタブ(MyViewController.m)に初期化処理追加
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Third", @"Third");
        self.tabBarItem.image = [UIImage imageNamed:@"third"];
    }
    return self;
}

if文のとこを追加。
下のバーに出る絵(.png)のところを登録している。