Aggressor
Aggressor

Reputation: 13551

Instantiating UIViewController Without A Storyboard Or xib File

I have been looking for a a way to instantiate a custom UIViewController class which is not attached to a storyboard or xib file.

Naturally, when I try to initialize the UIViewController it's expecting an NSCoder object.

However I have no idea where this NSCoder object comes from or how to properly make it. The documentation says its an interface declaration object.

Do you know how I can make an NSCoder object that will let me initialize my UIViewController or is there an NSCoder object somewhere in the application I can fetch?

public class Controller:UIViewController, UIAlertViewDelegate
{
    var _viewAuto:AutoUI! //newName

    override init()
    {
        super.init()
    }

    required public init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

enter image description here enter image description here

Upvotes: 3

Views: 2712

Answers (1)

kellanburket
kellanburket

Reputation: 12843

init(coder aDecoder: NSCoder) is only ever called when a view is instantiated from a storyboard. To create a custom UIViewController and instantiate in manually, all you need to do is add:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

to your custom view controller. Now you're free to create your own initialization method and pass it whatever variables you like.

Upvotes: 1

Related Questions