简体   繁体   中英

ViewDidLoad not called on custom objects in Storyboard

I finally made the switch to Storyboards and i am having issues loading custom controllers which was pretty easy to do when using interface builder.

I have ViewControllerOne with two components: A UIView and UITableView as the subview.

I want the UITableView to be controlled by a custom tableview controller. If this was Interface builder i would have dropped a tableview controller onto the XIB, linked to the custom controller and made the connections and it would have been done.

Using storyboard, i don't believe its possible to drop a UIViewController/UITableViewController onto a scene which already has a view controller, i relied on Objects to achieve this.

So i added a Object onto the scene and linked it to my custom tableview controller. I set up delegate/date source for my UITableView to point to the custom controller. I finally connected the UITableViews outlet to the custom controller.

When i compile this, the custom controllers delegate (for the table view) gets called but the viewDidLoad is never called.

The only way i can invoke viewDidLoad is if i move the UITableView out of ViewControllerOne. My understanding was that even though there is one view controller for a scene i can still manipulate the subviews using custom controllers.

Am i misunderstanding something or is there is a solution for this ?

Some screenshots 的TableView

在此处输入图片说明

在此处输入图片说明

There is a bit of magic in that. Call self.view from awakeFromNib and flow will back to the rails

- (void)awakeFromNib
{
    [super awakeFromNib];
    // here comes the magic - call self.view and view will load as expected
    NSLog(@"awakeFromNib %@", self.view)
}

you can call it from initWithNibName:bundle:

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSLog(@"awakeFromNib %@", self.view);
    }
    return self;
}

the point is to call self.view because apparently something is done inside.

If I have understood your question correctly:

1 Open the storyboard and navigate to the table view controller that you would like to be of your custom type.

2 Click on the identity inspector in the right hand side panel.

3 Set the class to what it should be.

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