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

0

您是否将 NavigationItem 设置为 NavigationBar?看起来你还没有在你的代码片段中完成它。

正如您在创建 Navigationitem 之前的日志中所说:

Nav bar's item is: <UINavigationItem: 0x7586a60>

之后,还是一样:

Now Nav bar's item is: <UINavigationItem: 0x7586a60>
于 2013-10-09T02:06:51.110 回答