その後のその後

iOSエンジニア 堤 修一のブログ github.com/shu223

UINavigationItemのleftBarButtonItemとbackBarButtonItemの違い

自分の手元にある古いメモ書きを見ると、下記のようにありました。

leftBarButtonとbackBarButtonの違い

  • 形が違う(leftは左側がとがった矢印形、backは四角形)
  • self.navigationItem.leftBarButtonItem = someBarBtn; は現在のビューに反映されるが、self.navigationItem.backBarButtonItem = someBarBtn; は次のビューに反映される


・・・え、、、2つめのってほんと?


過去の自分が信用できず改めてドキュメントを確認してみました。
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationItem_Class/Reference/UINavigationItem.html


(backBarButtonItem)

The bar button item to use when this item is represented by a back button on the navigation bar.

Discussion
When this item is the back item of the navigation bar―when it is the next item below the top item―it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.


(leftBarButtonItem)

A custom bar item displayed on the left of the navigation bar when this item is the top item.


んーたぶんそんなことは書いてない。
たぶん一番上に書いたメモ書きは間違ってて、コードを書く場所がpushする直前だったりそうじゃなかったりしたせいでそういう勘違いをしたのではなかろうか。



おまけ:UINavigationControllerのBackボタンの表示文字列を、前画面のタイトルでなく"Back"や「戻る」に変更する

何の役にも立たないエントリーになってしまったのでおまけ。


既にいろんな方が書かれているTipsですが、最初はなかなかやり方わからないのでこちらにも載せておきます。


下記処理をpushViewController:する直前あたりで行います。

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]
		initWithTitle:@"戻る"
		style:UIBarButtonItemStyleBordered
		target:nil
		action:nil];
self.navigationItem.backBarButtonItem = backButtonItem;