0
此任務看起來非常簡單。你可以從下面的日誌中看到,我檢查它是否設置成功,它是。那麼有什麼想法爲什麼當導航控制器推到這個視圖時它不會顯示?它與navigationItem和[navigationBar items]之間的區別有什麼關係?UIBarButtonItem在我成功將其添加到navigationitem後未顯示
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>
但沒有什麼,除了默認的後退按鈕導航欄顯示出來。
非常感謝您的幫助,