ios中的实现属性显示在图形界面上进行修改的知识点

属性能够在Interface Builder的图形化界面进行设置


代码与代码之间的桥梁是两个关键字:IB_DESIGNABLE 和 IBInspectable .

IBInspectable 可以定义动态属性,即为:可以在attribute inspector 面板中看到这个属性。可以将自定义的代码实时渲染到Interface Builder中.

注意:这个类必须是UIView 或者NSView的子类。(即为这个类必须是可以显示在interface building上的)
使用的时候导入相关的类。


相应的代码:

ViewButton.h 文件:

#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface ViewButton : UIButton

@property (strong, nonatomic) IBInspectable UIColor *borderColor;
@property (copy, nonatomic) IBInspectable NSString *log;

@end

ViewButton.m 文件

ViewButton.m 文件:

#import "ViewButton.h"
//
@implementation ViewButton

- (void)setBorderColor:(UIColor *)borderColor
{
    _borderColor = borderColor;
    self.layer.borderColor = borderColor.CGColor;
    
    //由于默认的button的变现是0.0f,所以会看不见
    if(self.layer.borderWidth ==0)
        self.layer.borderWidth = 1.0f;
}

- (void)setLog:(NSString *)log{
    _log = log;
    NSLog(@"%@",self.log );
}

@end

先关的代码关系截图:

创建的这个类,继承UIButton,并且还有这对关键字的书写位置。


在storyboad中使用的时候:(注意全出来的时候设置)



可以看到自己添加上去的自定义的属性:


运行之后的结果:(可以看到按钮的边框颜色的改变)


经常会出现的问题:

2015-11-17 20:48:58.648 IBInspectable[14596:352430] Failed to set (color) user defined inspected property on (UIButton): [<UIButton 0x7f83baf40cf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key color.
2015-11-17 20:48:58.649 IBInspectable[14596:352430] Failed to set (borderColor) user defined inspected property on (UIButton): [<UIButton 0x7f83baf40cf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key borderColor.
2015-11-17 20:48:58.649 IBInspectable[14596:352430] Failed to set (log) user defined inspected property on (UIButton): [<UIButton 0x7f83baf40cf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key log.
2015-11-17 20:48:58.650 IBInspectable[14596:352430] Failed to set (viewTitleText) user defined inspected property on (UIButton): [<UIButton 0x7f83baf40cf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key viewTitleText.
2015-11-17 20:48:58.650 IBInspectable[14596:352430] Failed to set (titleColor) user defined inspected property on (UIButton): [<UIButton 0x7f83baf40cf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key titleColor.

出现这些问题的原因是:我们在增加了这些属性之后,后来在代码中删除掉了,但是还是在UIStoryboard中有所保留。

所以需改掉相应的属性。


相关的操作可以看西面的图:



改了之后的结果:


如果其他的都没有问题,但是我们没有显示结果,有可能我们删除了相关的属性,或者没有添加上相应的属性,同样需要补全属性(如上面改变之后的结果)。
或者重新在上面设置属性就好了,(这样代码运行的时候的都是可以自动加载上去的)

因为删除了相关的设置值之后,我们将会看到我们开始设置的地方时默认的值。也就是没有设置值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值