Custom Init for Uiviewcontroller in Swift with Interface Setup in Storyboard

How to use custom init of ViewController in Storyboard

It's not the best idea to use this approach. At first, I want to suggest you to set this property after instantiation; it would be better

If you anyway want to make such constructor, you can place your code with instantiation to it, so it will look like

-(id) initWithoutAppointment
{
self = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"addNewPatientVC"];
if (self) {
self.roomBedNumberField.hidden = true;
}
return self;
}

but it won't be a good code

EDITED

May be it's the question of style, but I would rather prefer not to do this because view controller doesn't have to know about UIStoryboard; if you want to have such method, it would be better to move it to some separate factory.
If I chose to use this VC in other projects without Storyboard, or with storyboards, but with another name, it will be error-prone.

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)
}
}

How do I call a custom init on a view controller instantiated from a Storyboard scene in interface builder?

If you want to specify in Interface Builder, declare mediaType as a property in your UIViewController subclass, and use 'User Defined Runtime Attributes'.

screenshot

Using Storyboard and custom View Controller init

View controllers in storyboards are always initialised using

init?(coder aDecoder: NSCoder)

there's no way around that.

An alternative would be to have…

class DummyVC: UIViewController {
private var dummyManager: DummyManager!

func configure(dummyManager: DummyManager) {
self.dummyManager = dummyManager
}
}

and then…

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let dummyVC = mainStoryboard.instantiateViewControllerWithIdentifier("DummyVC") as! DummyVC
dummyVC.configure(dummyManager: DummyManager())

or

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

switch segue.destination {
case let dummyVC as DummyVC:
dummyVC.configure(dummyManager: DummyManager())
default:
break
}
}

Whilst not perfect (using let rather than var) the property being private and an implicitly unwrapped optional means it must be set (or the app will crash on use), and that can only happen from within the containing class.

I've adopted this throughout my apps, and find it quite a nice way to ensure all properties are set. Just remember to update the configure func when a property is added to a class.

viewController custom init method with storyboard

The designated initializer for view controllers in storyboards is -initWithCoder:. Since most view controllers from a storyboard are created via segues, you usually see state set during -prepareForSegue:sender:.

If you're instantiating a view controller directly from the storyboard like in the example you provided, then the pattern you've selected is appropriate.

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 use init for view controller loaded from storyboard?

I don't think so it will be useful if you create own init() for ViewController.

You can simply use like below:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
controller.pageTitle = "XYZ"
controller.pageNumber = 2

If you want to pass multiple values to the destination controller, then you can pass the model class as well if all the values define a category class.



Related Topics



Leave a reply



Submit