Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios8 IB_可外部设计_Ios8_Interface Builder_Ibdesignable - Fatal编程技术网

Ios8 IB_可外部设计

Ios8 IB_可外部设计,ios8,interface-builder,ibdesignable,Ios8,Interface Builder,Ibdesignable,我一直在研究IBU可设计性,这是一个非常棒的特性。然而,我对它的一个问题是,对于每个视图,我必须对UIView进行子类化。在该子类中,我必须指定initWithCoder:method和PrepareForPrinterFaceBuilder中的UI元素: 但是,我希望能够在类之外指定UI元素 我想创建一个UIView子类,它有一个公共属性,我可以从外部设置该属性,并在Inter face builder中查看 这可能吗 更新: 我意识到我是不可预见的。这使属性在属性检查器中可用。这不是我所说的

我一直在研究IBU可设计性,这是一个非常棒的特性。然而,我对它的一个问题是,对于每个视图,我必须对UIView进行子类化。在该子类中,我必须指定initWithCoder:method和PrepareForPrinterFaceBuilder中的UI元素:

但是,我希望能够在类之外指定UI元素

我想创建一个UIView子类,它有一个公共属性,我可以从外部设置该属性,并在Inter face builder中查看

这可能吗

更新:

我意识到我是不可预见的。这使属性在属性检查器中可用。这不是我所说的外部

IB_DESIGNABLE
@interface DesignableLabel : UILabel
@end
@implementation DesignableLabel 
@end

@interface SomeClass : UIViewController {
    IBOutlet DesignableLabel *DL;
}
@end
@interface SomeClass
- (void)prepareForInterfaceBuilder {
    DL.text = @"Hello World"
}
@end

DesignableLable是UILabel的一个子类。UILabel具有文本属性。我想让某个类以某种方式设置该属性。通常,如果我这样做并运行应用程序,它应该显示字符串。但是,为了省去运行应用程序的麻烦,我希望它显示在Interface builder中,以便在键入时可以看到它的值。

要在外部设置的公共属性(使用公共API)必须用
IBInspectable
关键字标记。 例:


您可以在上找到有关此关键字的更多信息。

我将回答自己的问题,以便其他人受益。您可以使用用户定义的运行时属性调用category方法,从外部设置IB_可设计对象

IB_DESIGNABLE
@interface DesignableLabel : UILabel
@end

@implementation DesignableLabel 
@end

@implementation DesignableLabel (Runtime_TheTextYouWant)
    - (void)setTheText:(BOOL)b {
        if (!b) return;
        self.text = "The text you want";
    }
@end

@implementation DesignableLabel (Runtime_SayHelloToMyLittleFriend)
    - (void)setMyLittleFriend:(BOOL)b {
        if (!b) return;
        self.text = "Say hello to my little friend";
    }
@end
我为我的DesignableLabel创建了两个类别。现在,如果我将两个UILabel放置在情节提要中的ViewController中,并将两个UILabel子类化为DesignableLabel,那么对于第一个UILabel,我可以通过调用键路径使用用户定义的运行时属性

theText
并设置复选标记。然后,该特定的DesignableLabel对象将在setTheText:category方法中指定文本

类似地,对于第二个DesignableLabel对象,可以通过调用键路径使用用户定义的运行时属性

myLittleFriend
请注意,键路径不使用单词“set”,而是将第一个单词的大小写降低。setMyLittleFriend:成为myLittleFriend


总之,这样可以避免为每个IB_可设计对象创建子类。

这涉及到向属性检查器添加属性。这不是我真正想要的。。。请看我的更新。希望它能澄清一些事情。。。
myLittleFriend