这项任务看起来非常简单。您可以从下面的日志中看到我检查它是否已成功设置并且确实如此。所以任何想法为什么当导航控制器推到这个视图时它没有显示出来?它与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>
但除了默认的后退按钮外,导航栏中没有显示任何内容。
非常感谢你的帮助,
答案 0 :(得分:0)
您是否将NavigationItem设置为NavigationBar? 看起来你还没有在你的代码片段中完成它。
正如您在创建Navigationitem之前的日志中所说:
Nav bar's item is: <UINavigationItem: 0x7586a60>
之后,仍然是相同的:
Now Nav bar's item is: <UINavigationItem: 0x7586a60>