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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:08:14  来源:igfitidea点击:

How to create the UIView sub class with xib?

iphoneiosxcodeuiview

提问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

  1. You need to create your subclass: File -> New -> File -> Cocoa Touch -> Objective C-class -> Subclass of Target Class Type(UIView on your case)
  2. Then, you create the xibfor it: File > New > User Interface > View(name your xib)
  3. Now go and design your xibon the Interface Builder.
  4. 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.
  1. 您需要创建您的子类:(File -> New -> File -> Cocoa Touch -> Objective C-class -> Subclass of Target Class Type您的案例上的 UIView)
  2. 然后,您xib为它创建:(File > New > User Interface > View命名您的 xib)
  3. 现在去设计你xibInterface Builder.
  4. 你应该做的最后一件事是改变你的观点的类类型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 并在对象检查器中更改它的类名。

enter image description here

在此处输入图片说明

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 访问,而只能通过代码访问。