简体   繁体   English

创建一个View Controller模板,以便我可以在整个iPhone应用程序中使用它

[英]Create a View Controller Template so that I can use it through out the my iphone app

I want to create a template view controller which includes these things: 我想创建一个包含以下内容的模板视图控制器:

  1. A navigation bar which is throughout the app 整个应用程序中的导航栏
  2. A header which is used throughout the app 在整个应用程序中使用的标题
  3. A standard background (the background of the view controller should be same throughout the app) 标准背景(整个应用程序中,视图控制器的背景应相同)
  4. A footer which is used throughout the app 在整个应用程序中使用的页脚

The bottom line is I need a custom view controller class which has a navigation bar, a header, a footer and a background. 最重要的是,我需要一个自定义视图控制器类,该类具有导航栏,页眉,页脚和背景。 I will then replace the all view classes to this class in the interface builder. 然后,我将在界面生成器中将所有视图类替换为该类。

Well, yes, you would create a custom view controller as a subclass of UIViewController. 好吧,是的,您将创建一个自定义视图控制器作为UIViewController的子类。 You may want to do the same with UITableViewController. 您可能要对UITableViewController进行相同的操作。 Lets assume you name them myViewController and myTableViewController. 假设您将它们命名为myViewController和myTableViewController。

Your interface builder views would then refer to this classes or their sub classes respectively. 然后,您的界面构建器视图将分别引用此类或它们的子类。

BackgroundColor: However, the view controllers to not have a background color them selfs. BackgroundColor:但是,视图控制器自身不能具有背景色。 Their view does have a background color and the view controller may set its view's background color with some constant value. 他们的视图确实具有背景色,并且视图控制器可以将其视图的背景色设置为某个恒定值。 Make sure that all views in the view hierarchy who's background color should be the same as the general background do not have a background color themselfs. 确保视图层次结构中所有背景颜色与背景相同的视图本身都没有背景颜色。 (or the clear color respectively: [UIColor clearColor] ) (或分别为透明色: [UIColor clearColor]

You can then set the color in your view controller like this: 然后,您可以像这样在视图控制器中设置颜色:

self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5];

Bare in mind that there are smarter ways of coding this rather than having the values hard coded at many places in the code. 切记,有比这在代码中的许多地方都硬编码值的方式更聪明的编码方式。

NavigationBar: The navigation bar is a property of the navigation controller. NavigationBar:导航栏是导航控制器的属性。 Usually there is only one navigation controller in the app (unless it is a tab bar based app, then there may be more). 通常,应用程序中只有一个导航控制器(除非它是基于标签栏的应用程序,否则可能会有更多)。 Therefore there is no need for subclassing all view controllers just to aim for a common custom navigation bar. 因此,无需仅针对通用的自定义导航栏就对所有视图控制器进行子类化。

Header and Footer: Frankly I am not sure what you mean by that. 页眉和页脚:坦白地说,我不确定您的意思。 You may refer to table view headers and footers. 您可以参考表格视图的页眉和页脚。 If so then, als already mentioned, an application wide superclass "myTableViewController" may be your choice, which provides common headers and footers along the app. 如果是这样,就像已经提到的那样,可以选择一个应用程序范围的超类“ myTableViewController”,它在应用程序中提供了常见的页眉和页脚。

The navigationBar is part of the UINavigationController which your viewControllers will be added to so this will automatically be standard across them. navigationBar是UINavigationController一部分,您的viewControllers将添加到其中,因此这将自动成为它们的标准配置。

As for creating the custom view controller, you are essentially asking "How do I do inheritance" which is outside the scope of a single answer. 至于创建自定义视图控制器,您实际上是在问“我如何做继承”,这超出了单个答案的范围。 There are plenty of good tutorials out there if you do a google search. 如果您使用Google搜索,这里有很多很好的教程。

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

相关问题 如何在iPhone应用程序中使用2选择器视图? - How can i use 2 picker view in my iPhone app? 如何在不引入View或Tab控制器的情况下将核心位置数据集成到我的空白iPhone App中? - How can I integrate core location data to my blank iPhone App without introducing View or tab controller? 如何在iPhone设备文件系统中存储图像,以便以后我可以检索它并在我的应用程序中使用? - How can I store an image in the iPhone device file system so that later if I want I can retrieve it and use in my app? 如何构建 ICU 以便我可以在 iPhone 应用程序中使用它? - How to build ICU so I can use it in an iPhone app? 如何向我的 iphone 应用程序添加额外的“视图”? - How can I add an additional "view" to my iphone app? 如何在iPhone应用程序中为此自定义视图转换设置动画? - How can I animate this custom view transition in my iPhone app? 如何通过我的iPhone应用程序创建警报 - How to create alarm through my iPhone app 如何在我的网页上为iPhone用户创建有应用程序的注释? - How can i create a note on my webpage for iphone user that there is an app? 如何为我的iPhone应用程序创建示例SQLite数据库? - How can I create a sample SQLite DB for my iPhone app? 如何在iPhone上为控制器分配视图? - How can I assign a view to a controller on iPhone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM