Custom Init of Uiviewcontroller from 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.

swift - Initialize view controller from storyboard by overriding init

A convenience initializer must always delegate to a designated initializer for the same class, and a designated initializer must call a superclass initializer.

Since the superclass doesn't have an appropriate initializer, you would probably be better served by a class factory method:

static func instantiate() -> SearchTableViewController
{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchTableViewController") as! SearchTableViewController
}

then use:

var myViewController = SearchTableViewController.instantiate()

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

How to Implement ViewController custom init using dependency injection and factory patterns?

There are different types of dependency injection. You're currently trying to use constructor-based dependency injection, which unfortunately doesn't really work with storyboards, since they need to initialise with a decoder. iOS 13 does introduce some additional functionality which will make this approach possible, but for the moment, you could use setter-based dependency injection instead.

Something like the following:

class ViewController: UIViewController {
var factory: ViewControllerFactory!
}

protocol ViewControllerFactory {
func makeViewController() -> ViewController
}

class DependencyContainer {
let storyboard: UIStoryboard = UIStoryboard(name: "Storyboard", bundle: Bundle.main)
}

extension DependencyContainer: ViewControllerFactory {
func makeViewController() -> ViewController {
guard let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController else {
fatalError("Unrecognised viewController")
}
vc.factory = self
return vc
}
}

Storyboard and custom init

I would just create a method which does the custom data loading.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
[myViewController loadCustomData:myCustomData];
[self presentViewController:myViewController animated:YES completion:nil];

If all your initWithCustomData method does is set one instance variable, you should just set it manually (no custom inits or extra methods required):

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
myViewController.iVarData = myCustomData;
[self presentViewController:myViewController animated:YES completion:nil];

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


Related Topics



Leave a reply



Submit