How to Make a Custom Initializer For a Uiviewcontroller Subclass in Swift

How do I make a custom initializer for a UIViewController subclass in Swift?

class ViewController: UIViewController {

var imageURL: NSURL?

// this is a convenient way to create this view controller without a imageURL
convenience init() {
self.init(imageURL: nil)
}

init(imageURL: NSURL?) {
self.imageURL = imageURL
super.init(nibName: nil, bundle: nil)
}

// if this view controller is loaded from a storyboard, imageURL will be nil

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

Swift: Custom ViewController initializers

Solved it! One has to call the designated initializer which in this case is the init with nibName, obviously ...

init(leftVC:UIViewController, rightVC:UIViewController, gap:Int)
{
self.leftVC = leftVC
self.rightVC = rightVC
self.gap = gap

super.init(nibName: nil, bundle: nil)

setupScrollView()
setupViewControllers()
}

How to subclass a UIViewController and add properties in swift?

At some point the view controller must be initialized by calling init(nibName:bundle:) or init(coder:)

Try this:

class MyViewController: UIViewController {

var prop: Int

init(prop: Int, nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
self.prop = prop
super.init(nibName:nibNameOrNil, bundle: nibBundleOrNil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

Swift: Initialization of UIViewController subclass with custom parameters

You can write class func for your view controller:

class MyViewController {
// ...
class func instantiate(dataSource: MyDataSource, cellAndHeaderManager: MyCellAndHeaderManager) -> MyViewController {
let vc = UIStoryboard(name: "Storyboard", bundle: nil).instantiateViewControllerWithIdentifier("MyViewController") as! MyViewController
vc.dataSource = dataSource
vc.cellAndHeaderManager = cellAndHeaderManager
return vc
}
}

So you can instantiate it with:

let vc = MyViewController.instantiate(dataSource: dataSource, cellAndHeaderManager: cellAndHeaderManager)

You can not instantiate view controller from Storyboard with initializer because there is no suitable initializer in UIViewController.

How do I write a custom init for a UIView subclass in Swift?

The init(frame:) version is the default initializer. You must call it only after initializing your instance variables. If this view is being reconstituted from a Nib then your custom initializer will not be called, and instead the init?(coder:) version will be called. Since Swift now requires an implementation of the required init?(coder:), I have updated the example below and changed the let variable declarations to var and optional. In this case, you would initialize them in awakeFromNib() or at some later time.

class TestView : UIView {
var s: String?
var i: Int?
init(s: String, i: Int) {
self.s = s
self.i = i
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}

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

How can I initialize a variable for UIViewController class in right syntax?

The required initializer is not the right one -- because your class is a subclass of UIViewController, you need a required init?(coder: NSCoder). You can put your custom initializer that sets backgroundColor in separate init.

Also, instead of viewDidLoad, use loadView for your custom View Controllers that you make in code. This is how you do it:

class UIViewControllerModel: UIViewController {

var backgroundColor: UIColor

/// Put your custom argument labels here, not inside the `required init?`
init(backgroundColor: UIColor) {
self.backgroundColor = backgroundColor
super.init(nibName: nil, bundle: nil)
}

/// This is in case the View Controller is loaded from the Storyboard
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

/// Use this instead of viewDidLoad
override func loadView() {

/**
Instantiate the base `view`.
*/
view = UIView()
view.backgroundColor = backgroundColor

}

}
let modelVC = UIViewControllerModel(backgroundColor: UIColor.blue)
self.present(modelVC, animated: true)

Result:

View controller with blue background presented

Swift 2.0 Subclassing a subclass of UIViewController and called convenience initializers

You need to make your initializers designated, not convenience:

class A : UIViewController {
init() {
super.init(nibName:nil, bundle:nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("")
}
}

class B : A {
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("")
}
}

That gives you the inheritance structure you're looking for.



Related Topics



Leave a reply



Submit