简体   繁体   中英

Custom subview in UIViewController not appearing

I created an objective-c class "customTable.h" subclassing UIView.

I then created a custom View in 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. In the customTable View in the xib I added a couple UILabels and a subview. For the subview I selected it and went under custom class and chose another custom class '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.

in the customTable.m file I have:

- (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." In the viewcontroller's .h file I linked this view as an IBOutlet.

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

When I run it in simulator the subview doesn't show up in the viewcontroller. I added some break points to the customTable.m file and the initialize methods are being called. 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. BTW, capitalize your class CustomTable since it's class.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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