[Swift 1] Day 18 - Custom UIView from .xib file


Start the Day 18 Lecture

Post any comments, questions, or issues here.



I am making updates to an app that I am working on. What I have been missing was the ability to use different views. (I still am having some issues)

But for now, I am trying to move an existing functionality to my new UIView I have create after this tutorial.

What I am trying to do is connect a button to an action in another file. Is that possible? I am trying to use a function that is on my ViewController.swift file with a button I have placed in my new custom UIView.

When I tap the button, I want to have something happen on the view controller screen.

I hope I am making sense. I can email you the entire project to see what I am trying to do.



The next lesson I’m working on for Day 19 will show multiple screens and how to communicate between code files.

Email me the project to clarify what you’re trying to do.


Hi, Paul.

I noticed the following behavior in IBDesignable classes: override func prepareForInterfaceBuilder() does nothing when verifying a single .xib file in Interface Builder… I mean: Interface Builder does live render .xib files inside Storyboards, but not .xib files themselves… Does that make sense?

To make my question more clear I take one of your lectures as an example (day 18 / 18-6).
I uncommented line 89:

override func prepareForInterfaceBuilder() {
// setup anything that you need
view.backgroundColor = UIColor.blueColor()

It works fine when selecting the Storyboard:

But when selecting .xib file:

Now taking my project as example:

I wonder: do you think this is by design? Maybe I’m missing something. Is there any way to live render .xib files alone?



Yes it’s limited to Storyboard files. Submit a bug report and ask for support in a xib file. http://bugreport.apple.com



rajeejones, DId you ever figure out how to launch a func in view controller from a button loaded from .xib? I’m trying to do the same thing and can’t seem to figure it out. Thanks in advance!


@jasan There are two ways to do it:

1: You can have the function of type IBAction. Open the view controller and xib in assistant editor and control drag from the button to the function (or drag to a empty spot and select IBAction and name your function)

2: you can have the button conform to a protocol. This is useful when you have a custom view that is not apart of the view controller