在 Interface Builder 中设计视图,但在代码中定义一些逻辑
我想创建一个简单的“控件”,其中定义了 4 个 UILabels。一方面,我想使用 Interface Builder 来布局我的设计,但另一方面,我想公开一些将定义每个标签内容的属性 - 这可能吗?
@interface CompositeView : UIView{
int numberOne;
int numberTwo;
}
@property(nonatomic,assign) int numberOne; //set from view controller and reflected by first UILabel text
@property(nonatomic,assign) int numberTwo; //set from view controller and reflected by second UILabel text
现在理想情况下我想做这样的事情:
compositeView.numberTwo=9232;
在我的视图控制器中。 问题是..如何在IB中设计视图的外观,但在代码中公开/实现一些必要的逻辑?一种想法是在 CompositeView 中加载一个定义了视图的 NIB 文件,并将此类视图添加为子视图。但这感觉像是在与框架作斗争。
I want to create a simple "control" that will have 4 UILabels defined in it. On one hand I'd like to use Interface Builder to layout my design but on the other hand i'd like to expose some properties that will define contents of each label - is it possible?
@interface CompositeView : UIView{
int numberOne;
int numberTwo;
}
@property(nonatomic,assign) int numberOne; //set from view controller and reflected by first UILabel text
@property(nonatomic,assign) int numberTwo; //set from view controller and reflected by second UILabel text
Now ideally I want to do things like:
compositeView.numberTwo=9232;
in my view controller.
The problem is .. how do I design view's appearance in IB but expose/implement some of the neccessary logic in code? One idea is to load a NIB file in CompositeView with a view defined and add such view as a subview.. but it feels like fighting with the framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的属性设置为“IBOutlet”,然后将其连接到 Interface Builder 中的标签。您可能必须将它们设为字符串并显式进行转换,但连接要在 Interface Builder 中显示的值是其强项之一。 IBOutlet 用于您希望 Interface Builder 能够看到的属性,IBActions 用于您希望能够从 Interface Builder 的 UI 控件触发的方法。
我建议通过一些示例并跟踪接线来理解 IBOutlet 和 IBAction。
Make your property an "IBOutlet" then wire it up to the label in Interface Builder. You'll probably have to make them strings and do the conversion explicitly, but wiring up a value to be displayed in Interface Builder is one of its strong suits. IBOutlets are for properties you want Interface Builder to be able to see, and IBActions are for methods you want to be able to trigger from a UI control from Interface Builder.
I'd recommend going through some examples and tracing the wiring to understand IBOutlet and IBAction.