这个任务看起来很简单。您可以从下面的日志中看到,我检查它是否设置成功。那么知道为什么当导航控制器推送到这个视图时它没有出现吗?它与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>
但是除了默认的后退按钮之外,导航栏中没有显示任何内容。
非常感谢你的帮助,