RTzhong
RTzhong

Reputation: 205

add a custom uiview with xib in my storyboard

I am using swift2.0 and I have just added a custom UIView called SortableTableTitleView:

enter image description here

enter image description here

and then I drag a UIView in my storyboard, and I changed the class of the view to SortableTableTitleView

enter image description here

When I tried to set the text of the label in the custom view, I just got a bad access error.

I tried to write the initwithcoder method of the view, but I don't know how I can load the element from xib. (It is somehow different with ObjectiveC). So, how can I solve this?

enter image description here

Upvotes: 0

Views: 194

Answers (1)

Robert J. Clegg
Robert J. Clegg

Reputation: 7370

You cannot use xib files within the Storboard file. So either create the view fully in the Storyboard and use it as per normal. Or, add the view from your xib file add a subView to the view on the storyboard via code.

So something like this in the viewController:

UINib *nibFile = [UINib nibWithNibName:@"SortableTableTitleView" bundle:nil];
            NSArray *views = [nibFile instantiateWithOwner:self options:nil];
            UIView *sortableTableTitleView = views[0]; //Assuming it is the only view in the xib file

//Add it where you want as a normal view now: 
[self.view addSubview: sortableTableTitleView];

I realise the code above is in objective-c - I'll get the Swift comparison when I get a gap (Being new to Swift and all)

Upvotes: 1

Related Questions