简体   繁体   English

Objective-c:如何在 UINavigationController 中的视图之间共享一个背景图像

[英]Objective-c: How to have a one shared background image between the views in UINavigationController

I'm trying to create a basic navigation app using UINavigationController as root container for other inner UIViewController views.我正在尝试使用 UINavigationController 作为其他内部 UIViewController 视图的根容器创建一个基本导航应用程序。 I have already achieved similar functionality with shared toolbar.我已经通过共享工具栏实现了类似的功能。 I'm wondering, is it a good approach because the toolbar and its items are redrawed (i think?) on every inner view.我想知道,这是一个好方法,因为工具栏及其项目在每个内部视图上都被重绘(我认为?)。 So, here are some details of my existing code.所以,这里是我现有代码的一些细节。 In my main MultiviewAppDelegate.m implementation file, in didFinishLaunchingWithOptions method, I am initializing UINavigationController, then initializing the first inner view (UIViewController) and then adding its view to UINavigationController.在我的主 MultiviewAppDelegate.m 实现文件中,在 didFinishLaunchingWithOptions 方法中,我正在初始化 UINavigationController,然后初始化第一个内部视图(UIViewController),然后将其视图添加到 UINavigationController。

UINavigationController *nc = [[UINavigationController alloc] init];
[self.window addSubview:nc.view]; 
...
FirstView *v1 = [[FirstView alloc] init];
[nc pushViewController:v1 animated:NO];
[nc setToolbarHidden:NO];
...

All other inner views (like FirstView) extends BaseViewController class (BaseViewController extends UIViewController) because, in my BaseViewController, I have implemented shared toolbar that I want to be visible across inner views.所有其他内部视图(如 FirstView)都扩展了 BaseViewController class(BaseViewController 扩展了 UIViewController),因为在我的 BaseViewController 中,我实现了共享工具栏,我希望在内部视图中可见。 So in the method viewDidLoad of BaseViewController class I'm doing the creation of that toolbar:因此,在 BaseViewController class 的 viewDidLoad 方法中,我正在创建该工具栏:

- (void)viewDidLoad {  
  UIBarButtonItem *firstViewBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"FirstView"                                                                     style:UIBarButtonItemStyleBordered target:self action:@selector(showFirstView)];  
  UIBarButtonItem *secondViewBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"SecondView"                                                                     style:UIBarButtonItemStyleBordered target:self action:@selector(showSecondView)];
  ...
  NSArray *items = [NSArray arrayWithObjects:firstViewBtnItem, secondViewBtnItem, ..., nil];  
  [firstViewBtnItem release];
  [secondViewBtnItem release];
  [self setToolbarItems:items]; 
  ...

The thing is I'm not sure is it good to redraw a toolbar every time every new inner view comes up.问题是我不确定每次出现每个新的内部视图时都重新绘制工具栏是否好。 So I'm not sure would it be a good decision to do the same with a background.所以我不确定对背景做同样的事情是否是一个好的决定。 Or maybe, I want too much:)或者,我想要的太多了:)

Have you tried just creating a view and adding it to your window before adding the navigation controller's view?在添加导航控制器的视图之前,您是否尝试过创建一个视图并将其添加到您的 window 中? Anyway, unless your background is cpu intensive I can't imagine it would be a problem including it in every 'inner view' - this also gives you more flexibility for the future if you need views without this background or with a slightly different background.无论如何,除非您的背景是 cpu 密集型的,否则我无法想象在每个“内部视图”中都包含它会是一个问题 - 如果您需要没有此背景或背景略有不同的视图,这也为您未来提供了更大的灵活性。

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

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