简体   繁体   English

UIViewController中的自定义子视图未出现

[英]Custom subview in UIViewController not appearing

I created an objective-c class "customTable.h" subclassing UIView. 我创建了UIView的Objective-C类“ customTable.h”子类。

I then created a custom View in XIB. 然后,我在XIB中创建了一个自定义视图。 I went to the identity inspector and under custom class I chose the name of the class I created (customTable) as file's owner. 我去了身份检查器,在自定义类下选择了我创建的类的名称(customTable)作为文件的所有者。 In the customTable View in the xib I added a couple UILabels and a subview. 在xib的customTable视图中,我添加了几个UILabel和一个子视图。 For the subview I selected it and went under custom class and chose another custom class 'menuTable.' 对于子视图,我选择了它并进入自定义类,然后选择了另一个自定义类“ menuTable”。

In the customTable.h file I have linked the menuTable from the XIB as an IBOutlet, because I want to be able to do some configuration of the outlet in the view's initialization. 在customTable.h文件中,我已将XIB中的menuTable链接为IBOutlet,因为我希望能够在视图的初始化中对插座进行一些配置。

in the customTable.m file I have: 在customTable.m文件中,我有:

- (id)init
{
    if(self = [super init])
    {
        [self initialize];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
         [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        [self initialize];
    }
    return self;
}
- (void)initialize
{
    /*some special configuration code for menuTable
    is here */
}

In Storyboard I added a subview to a ViewController's view and then told storyboard I wanted the subview to use the custom class "customTable." 在情节提要中,我向ViewController的视图添加了一个子视图,然后告诉情节提要,我希望该子视图使用自定义类“ customTable”。 In the viewcontroller's .h file I linked this view as an IBOutlet. 在viewcontroller的.h文件中,我将此视图链接为IBOutlet。

@property (weak, nonatomic) IBOutlet customTable *cView;

When I run it in simulator the subview doesn't show up in the viewcontroller. 当我在模拟器中运行它时,子视图不会显示在viewcontroller中。 I added some break points to the customTable.m file and the initialize methods are being called. 我在customTable.m文件中添加了一些断点,并调用了initialize方法。 So why is it not appearing? 那么为什么它不出现呢?

My suggestion is in your storyboard, drag drop the "Container View" on top of your main view. 我的建议是在您的情节提要中,将“容器视图”拖放到主视图顶部。 This will create a container with a connection to new view controller in your storyboard. 这将创建一个容器,该容器具有连接到故事板上的新视图控制器的连接。 You then select new view controller and set the custom view to your CustomTable in the identity inspector. 然后,选择新的视图控制器,然后在身份检查器中将自定义视图设置为CustomTable。 BTW, capitalize your class CustomTable since it's class. 顺便说一句,因为类CustomTable是大写的,所以要大写。

I think I figured it out. 我想我知道了。 The reason this was happening was that I was supposed to select "With XIB for user interface" when I created the class. 发生这种情况的原因是,我在创建类时应该选择“使用XIB作为用户界面”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM