如何加载 UIView 子视图



我正在尝试使用一个漂亮的控件,但无法弄清楚 a) 如何加载数据和 b) 如何将此控件转换为情节提要。

控件在这里

我加载子视图标题的代码工作正常。

-(NSString *)titleForCollapseClickAtIndex:(int)index {
    //NSLog(@"%s", __FUNCTION__);
    switch (index) {
        case 0:
            return @"Item Information";
            break;
        case 1:

我不知道如何加载下一个方法:

-(UIView *)viewForCollapseClickContentViewAtIndex:(int)index {
    NSLog(@"%s", __FUNCTION__);
    switch (index) {
        case 0:
            return  itemView;
            break;

(UIView *) ItemView(初始化后)为:

- (void)loadElements
{
    NSLog(@"%s", __FUNCTION__);
    [self.item setText: @"try this"];
    self.category.text = @"category";
    self.date1.text = @"todays date";
    self.date2.text = @"another date";
    self.serial.text = @"serial no";
} return self;

视图在 IB 中绘制,并与这些项目有关联。UIView 也绘制在主 xib 中。它像演示中一样显示,带有来自 ItemView 类的标题。

正如我所提到的,标题是从视图控制器继承的。文本(例如@试试这个",不要。.

如果我尝试在"viewForCollapseClickContentViewAtIndex"方法中记录文本字段,则会出现运行时异常:

2014-01-17 07:54:44.856 CollapseClickDemo[29037:70b] -[UIView item]: unrecognized selector sent to instance 0x8c84590
2014-01-17 07:54:44.858 CollapseClickDemo[29037:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView item]: unrecognized selector sent to instance 0x8c84590'
*** First throw call stack:
(
    0   CoreFoundation                      0x024531e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x015808e5 objc_exception_throw + 44
    2   CoreFoundation                      0x024f0243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0244350b ___forwarding___ + 1019
    4   CoreFoundation                      0x024430ee _CF_forwarding_prep_0 + 14
    5   CollapseClickDemo                   0x000034b8 -[ViewController viewForCollapseClickContentViewAtIndex:] + 152
    6   CollapseClickDemo                   0x0000ca89 -[CollapseClick reloadCollapseClick] + 1561

[self.item setText: @"try this"];

这是导致异常的原因。 self(似乎是您的控制)没有item属性。 self.item等效于[self item],并且由于没有名为item的方法,因此会得到无法识别的选择器异常。

一旦它工作,您可以通过向情节提要添加滚动视图(因为您的小部件是 UIScrollView 的子类)然后将其类更改为 CollapseClick 来将其添加到情节提要中。AFAIK,目前没有办法将用户定义的类添加到情节提要编辑器支持的类列表中。

最新更新