xcode 如何使用 xib 创建 UIView 子类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15836034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create the UIView sub class with xib?
提问by Mani
I want to create UIView commonly. And use that view in all the view controllers. How can create the UIView sub class with xib and use in all the view controllers? Thanks in advance.
我想通常创建 UIView。并在所有视图控制器中使用该视图。如何使用 xib 创建 UIView 子类并在所有视图控制器中使用?提前致谢。
回答by Bartu
- You need to create your subclass:
File -> New -> File -> Cocoa Touch -> Objective C-class -> Subclass of Target Class Type
(UIView on your case) - Then, you create the
xib
for it:File > New > User Interface > View
(name your xib) - Now go and design your
xib
on theInterface Builder
. - Last thing you should do is to change your view's Class Typein
Identity Inspector
. Your custom xib will be linked with your custom class.
- 您需要创建您的子类:(
File -> New -> File -> Cocoa Touch -> Objective C-class -> Subclass of Target Class Type
您的案例上的 UIView) - 然后,您
xib
为它创建:(File > New > User Interface > View
命名您的 xib) - 现在去设计你
xib
的Interface Builder
. - 你应该做的最后一件事是改变你的观点的类类型在
Identity Inspector
。您的自定义 xib 将与您的自定义类链接。
Let me know if you need further detail.
如果您需要更多详细信息,请告诉我。
回答by Stavash
A UIView subclass first of all must consist of some code defining it, regardless of how you're going to use this view. After you create this class, in order to use it you only need to define the class name in the Interface Builder. Select the UIView you added and change it's class name in the object inspector.
UIView 子类首先必须包含一些定义它的代码,无论您将如何使用此视图。创建此类后,为了使用它,您只需在 Interface Builder 中定义类名。选择您添加的 UIView 并在对象检查器中更改它的类名。
Keep in mind that new properties or functionality that you are adding to this UIView will not be accessible by editing the xib, but only by code.
请记住,您添加到此 UIView 的新属性或功能将无法通过编辑 xib 访问,而只能通过代码访问。