0

このタスクは非常に簡単に思えます。以下のログから、正常に設定されているかどうかを確認できます。では、ナビゲーション コントローラーがこのビューにプッシュしたときに表示されない理由はありますか? それは、navigationItem と [navigationBar items] の違いと関係がありますか?

    NSLog(@"Nav bar's item is: %@", [[[[[self navigationController] navigationBar] items] objectAtIndex:0] description]);
    NSLog(@"The navigationItem's Right bar button is: %@", [[[self navigationItem] rightBarButtonItem] description]);

    //create bar button item with segmented control as custom view
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    NSLog(@"Created bar button item: %@", barButtonItem);
    NSLog(@"Bar button item's view is: %@", [barButtonItem customView]);

    //add segmented control bar button item to navigation bar

    [[self navigationItem] setRightBarButtonItem:barButtonItem];
    NSLog(@"Added bar button item, %@, to nav item %@", barButtonItem, [self navigationItem]);
    NSLog(@"Now Nav bar's item is: %@", [[[[[self navigationController] navigationBar] items] objectAtIndex:0] description]);
    NSLog(@"Now the navigationItem's Right bar button is: %@", [[[self navigationItem] rightBarButtonItem] description]);

ログ

     Nav bar's item is: <UINavigationItem: 0x7586a60>
     The navigationItem's Right bar button is: (null)
     Created bar button item: <UIBarButtonItem: 0x90a2180>
     Bar button item's view is: <UISegmentedControl: 0x90a11e0; frame = (0 0; 101 44); opaque = NO; layer = <CALayer: 0x90a1150>>
     Added bar button item, <UIBarButtonItem: 0x90a2180>, to nav item <UINavigationItem: 0x909c5a0>
     Now Nav bar's item is: <UINavigationItem: 0x7586a60>
     Now the navigationItem's Right bar button is: <UIBarButtonItem: 0x90a2180>

ただし、デフォルトの [戻る] ボタンを除いて、ナビゲーション バーには何も表示されません。

ご協力ありがとうございました。

4

1 に答える 1