Skip to content

2.1 UINavigationController

tamotamago edited this page Apr 18, 2013 · 20 revisions

UINavigationController Class Reference | View Controller Programming Guide for iOS

概要

navigationController1

Source:View Controller Programming Guide for iOS

UINavigationController は UIViewController を階層的に管理するコンテナです。複数の UIViewController はスタックで管理されます。またその各 UIViewController にナビゲートするインタフェース(UINavigationBar, UIToolBar)も管理します。

navigationController2

Source:View Controller Programming Guide for iOS

UINavigationController における重要な property と method は以下の通りです。

  • topViewController - スタックのトップにいる viewController
  • viewControllers - viewController が含まれている NSArray
  • pushViewController:animated: - viewControllers スタックに push され画面遷移します
  • popViewController:Animated: - スタックから pop され画面遷移します
  • delegate methods を実装することで画面遷移の通知を受け取れます

実装

navigationController の表示、push

無限に push する画面遷移を実装しましょう。

MixiAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[MixiViewController alloc] initWithNibName:@"MixiViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

MixiViewController.h

#import <UIKit/UIKit.h>

@interface MixiViewController : UIViewController

- (IBAction)pressPushButton:(id)sender;

@end

MixiViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = [NSString stringWithFormat:@"%d", [self.navigationController.viewControllers count]];
}

- (IBAction)pressPushButton:(id)sender
{
    [self.navigationController pushViewController:[[MixiViewController alloc] init] animated:YES];
}

MixiViewController.xib xib

rootView の TopBar を NavigationBar にすると、NavigationBar が現れます。これは実際に NavigationBar を設置しているのでは無く、NavigationBar がある体でレイアウトをするという意味です。この ViewController には NavigationBar が入ってくるので、それを考慮して他のレイアウトを作ることが出来ます。

確認 : navigationController.viewControllers にスタックされていることを、ログで確認してください。

###問題 アプリ起動時で 5 階層目にいるように実装してください。 (HINT:setViewControllers:)

NavigationBar UINavigationItem

UINavigationBar Class Reference

UINavigationItem Class Reference

UINavigationBar は UINavigationItem を管理するためのコンテナです。UINabigationItem は各 ViewController が持っていて、NavigationBar に表示させる情報を管理しています。

Source : UINavigationBar

iOS View Controllerプロ グラミングガイド

NavigationBar 右上方にボタンを設置してみましょう。ボタンタップで pop を実装しましょう。

MixiVIewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = [NSString stringWithFormat:@"%d", [self.navigationController.viewControllers count]];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"pop" style:UIBarButtonItemStylePlain target:self action:@selector(pressPopButton)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)pressPushPopButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

カスタマイズ と UIAppearance protocol

UIAppearance Protocol Reference

UIAppearance を用いると、特定の UIComponent のデザインを一括して変更することが出来ます。

MixiAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self customizeAppearance];
    .
    .
    .
}

- (void)customizeAppearance
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"customNavBarImage1"] forBarMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setTintColor:[UIColor blackColor]];
}

画像データはこちら customNavBarImage1.png

詳しい実践は以下をご参照ください : iOS 5 ユーザーインターフェースのカスタマイズ

  1. 導入
  2. Objective C の基礎
  3. メモリ管理
  4. UIViewController1 - UIViewController のカスタマイズ
  5. UIViewController2 - ModalViewController
  6. UIViewController3 - ライフサイクル
  7. UIKit 1 - container, rotate-
  8. UINavigationController
  9. UITabController
  10. Custom Container View Controller
  11. Supporting Multiple Interface Orientations
  12. UIKit 2- UIView -
  13. UIView
  14. UIView のカスタマイズ
  15. UIView Animation
  16. UIKit 3 - table view -
  17. UITableView について
  18. UITableViewとNavigationController
  19. custom UITableViewCell の作成
  20. 4.4 UITableViewのその他のオプション、カスタマイズ
  21. UIKit 4 - images -
  22. UIImage (CoreGraphics)
  23. UIImageView
  24. Accets Library
  25. CoreImage
  26. UIKit 4 - text -
  27. UILabel
  28. UITextView
  29. KeybordNotification
  30. 非同期処理系
  31. NSURLConnection (json シリアライザ)
  32. Blocks
  33. GCD
  34. ローカルキャッシュ
  35. UserDefaults
  36. FileManager
  37. CoreData
  38. SQLite
  39. Instruments
  40. leak
  41. time profiler
  42. その他
  43. 単体テスト (GHUnit)
  44. 結合テスト (KIF)
  45. cocoaPods でライブラリ管理

edit sidebar

Clone this wiki locally