我相信我有“答案”......而且它是如此简单,我很尴尬,我以前没有想到它。创建 UIViewController 的自定义子类,在其中设置 UINavigationController 工具栏的属性(在 IB 的 xib 中设置为启用)。然后,从您希望该工具栏包含的任何其他视图中,您只需对该类进行子类化。在我的例子中,我命名了 UIViewController 的 ChallengeToolbar 子类,然后我这样做了:
UIImage *vaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"goldbar" ofType:@"png"]];
UISegmentedControl *vaultButtonControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:vaultImage, nil]];
vaultButtonControl.segmentedControlStyle = UISegmentedControlStylePlain;
vaultButtonControl.momentary = YES;
[vaultButtonControl addTarget:self action:@selector(goNavVault:) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *vaultButton = [[UIBarButtonItem alloc] initWithCustomView:vaultButtonControl];
vaultButton.customView.frame = CGRectMake(0,0,40,20);
UIBarButtonItem *invite = [[UIBarButtonItem alloc] initWithTitle:@"Invite" style:UIBarButtonItemStyleBordered target:self action:@selector(goNavVault:)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *items = [NSArray arrayWithObjects: invite, flexItem, flexItem, flexItem, vaultButton, nil];
[self setToolbarItems:items animated:NO];
[vaultButtonControl release];
[vaultButton release];
[invite release];
[flexItem release];
...在 viewDidLoad 方法中。然后,从我希望我的工具栏出现的任何视图中,我只需将视图的控制器类设置为我的 ChallengeToolbar 类而不是 UIViewController 的子类。呃!