[英]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: 我想创建一个包含以下内容的模板视图控制器:
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.