0

私のアプリケーションはUINavigationController. 単純にMyUIViewControlleras rootViewControllerofnavigationControllerを設定しcustomTitleViewnavigationItemof をmyViewControllerinsideに設定しましたviewDidLoad

今、私がプッシュするnewViewControllerと、前のものが表示されることを期待していますcustomTitleView(Apple NavigationController リファレンスで説明されているように) が、そうではありません。

どうしたの?

以下のコードの一部と Apple UINavigationController リファレンス

「新しい最上位ビュー コントローラーにカスタム タイトル ビューがある場合、ナビゲーション バーには、デフォルトのタイトル ビューの代わりにそのビューが表示されます。カスタム タイトル ビューを指定するには、ビュー コントローラーのナビゲーション アイテムの titleView プロパティを設定します。」

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitleView:customTitleView];
}

「デフォルトのタイトルビュー」は「何もない」ことを意味するのでしょうか?以前の titleView と解釈しました。

編集

回避策を試していますが、他にも問題があります。回避策は次のとおりです。

NavigationController 内にプッシュするすべての ViewController で、viewDidLoad で rootViewController から取得する titleView をセットアップします

UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]

newTitleView を作成し、originalContainer.subviews (UIButton) の中に入れます。そこから Target アクションが必要なためです。

すべてが最初にプッシュされたコントローラに対して完全に機能しますが、スタック内の別のviewControllerをプッシュすると(2番目にプッシュされたもの)、navigationControllerへのすべての参照が失われます。すべてのインスタンス変数は nil です。

この値は、viewDidLoad 内で取得されます

firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0 firstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0

secondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0 secondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0

secondViewControllerPushed はどこにもないようです!!

それはどのように可能ですか?

ビューコントローラーをモーダルに表示するのではなく、正しくプッシュすることを再確認しました

この問題が原因で、secondViewControllerPushed の newTitleView を正しく設定できません。

4

1 に答える 1

2

This is a hack in a half but I feel it gives you a bit more control in the viewController you are working in. So below is a small method I have setup and then I just call it in viewDidLoad - [self setUpNavBar];

And using [UIButton buttonWithType:101] is perfectly fine as it passed Apple Validation.

- (void) setUpNavBar
{
    [self.navigationController setNavigationBarHidden: NO animated:YES];

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Settings";


    UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" 
                                                                            style:UIBarButtonItemStylePlain target:self 
                                                                            action:@selector(logoutAction)] autorelease];                     

}
于 2011-11-21T17:26:21.097 に答える