Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclass ViewController from Storyboard

I created ViewController in Storyboard and I am using

instantiateViewControllerWithIdentifier:

to load it. But I need to have this VC as base class and use 3-4 subclasses to change its properties.

How can I get an instance of my subclass with instantiateViewControllerWithIdentifier?

like image 819
Mat Demon Avatar asked Sep 14 '15 01:09

Mat Demon


People also ask

How do I set the identifier for ViewController in storyboard?

Step 1: Set a Storyboard IDIn the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.

What is a UIViewController subclass?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


1 Answers

@Bhagyesh version in Swift 3:

class func instantiateFromSuperclassStoryboard() -> SubclassViewController {
    let stroryboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = stroryboard.instantiateViewController(withIdentifier: "BaseViewController")
    object_setClass(controller, SubclassViewController.self)

    return controller as! SubclassViewController
}
like image 90
ChikabuZ Avatar answered Sep 21 '22 11:09

ChikabuZ