简体   繁体   English

iPhone工具栏由多个视图共享

[英]iPhone toolbar shared by multiple views

The app I'm building needs to show a shared custom UIToolbar for multiple views (and their subviews) within a UITabBarController framework. 我正在构建的应用程序需要显示一个UITabBarController框架中多个视图(及其子视图)的共享自定义UIToolbar。 The contents of the custom toolbar are the same across all the views. 自定义工具栏的内容在所有视图中都是相同的。 I'd like to be able to design the custom toolbar as a xib and handle UI events from its own controller class (I'm assuming I can subclass UIToolbar to do so?). 我希望能够将自定义工具栏设计为xib并从其自己的控制器类处理UI事件(我假设可以对UIToolbar进行子类化吗?)。 That way I could define IBOutlet & IBAction items, etc. Then I could associate this custom toolbar with each of the UITabBarController views (and their subviews). 这样,我可以定义IBOutlet和IBAction项目等。然后,我可以将此自定义工具栏与每个UITabBarController视图(及其子视图)相关联。 But I'm having trouble finding out whether that's possible - and if so, how to do it. 但是我很难确定这是否可能-如果可以的话,该怎么做。

In particular, I want to be able to push new views onto UINavigationController view stacks which are each associated with parent UITabBarController tabs. 特别是,我希望能够将新视图推入UINavigationController视图堆栈,每个视图堆栈都与父UITabBarController选项卡相关联。 So, to summarize, I want a: 因此,总而言之,我想要一个:

  • custom toolbar 自定义工具栏
  • shared by multiple views 由多个视图共享
  • which are managed by multiple navigation controllers 由多个导航控制器管理
  • and the navigation controllers are associated with different tabs of a parent tab bar controller 并且导航控制器与父选项卡栏控制器的不同选项卡相关联

The tab bar controller itself is launched modally, though I don't believe that's relevant. 标签栏控制器本身是以模式启动的,尽管我认为这无关紧要。

Anyway, the tab bar controller is working, as are its child navigation controllers. 无论如何,选项卡栏控制器及其子导航控制器都在工作。 I'm just having a little trouble figuring out how to persist the shared toolbar to the various subviews. 我在弄清楚如何将共享工具栏持久化到各个子视图时遇到了一些麻烦。 I'd settle for a good clean way of implementing programmatically... though I'd prefer the flexibility of keeping the toolbar's visual design in a xib. 尽管我希望灵活地将工具栏的可视化设计保留在xib中,但我会选择一种以编程方式实现的良好简洁方法……

Anyone have any suggestions 任何人有什么建议

I believe I have the "answer"... and it's so simple, I'm rather embarrassed that it didn't occur to me before now. 相信我有“答案” ...而且它是如此简单,我很尴尬,因为它在我之前从未发生过。 Create a custom subclass of UIViewController, within which you set the attributes of the UINavigationController's toolbar (set as enabled from within the xib in IB). 创建UIViewController的自定义子类,在其中设置UINavigationController工具栏的属性(在IB的xib中设置为启用)。 Then, from whichever other views you want that toolbar included, you just subclass that class. 然后,从希望包含该工具栏的任何其他视图中,您就可以对该类进行子类化。 In my case, I named a ChallengeToolbar subclass of UIViewController, then I did this: 就我而言,我命名了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];

... within the viewDidLoad method. ...在viewDidLoad方法中。 Then, from any view in which I want my toolbar to appear I simply set up the view's controller class to subclass my ChallengeToolbar class instead of UIViewController. 然后,从我希望其工具栏出现的任何视图中,我只需将视图的控制器类设置为我的ChallengeToolbar类的子类,而不是UIViewController的子类。 DUH! H!

A few initial thoughts. 一些初步的想法。

You will probably want to keep this toolbar outside of the scope of the tabbar and just propagate events that happen from the toolbar to the view on screen. 您可能希望将此工具栏保持在选项卡范围之外,而只是将发生在工具栏上的事件传播到屏幕视图。

As it stands, the toolbar is managing several views and I do not believe you can have components floating on top of a particular view inside of the controller. 就目前而言,工具栏正在管理多个视图,我不认为您可以将组件浮动在控制器内部特定视图之上。

I typically code my UI programatic so I can have exact control, so I can't say how easy it is to manage this from Interface Builder. 我通常以程序化方式对UI进行编码,这样我就可以进行精确控制,因此无法说从Interface Builder进行管理很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM