Skip to content

2.1 UINavigationController

tamotamago edited this page Apr 11, 2013 · 12 revisions

UINavigationController Class Reference | View Controller Programming Guide for iOS

概要

navigationController1

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

navigationController2

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];
}

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

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

NavigationBar UINavigationItem

UINavigationBar Class Reference

UINavigationItem Class Reference

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

UINavigationBar

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:nil];
    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]];
}

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

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

edit sidebar

Clone this wiki locally