0

I have an array UIButtons that have a unique tag value. When a given button is pressed, I want to load and present a UIViewController that is also stored in an array of equal length.

The UIViewControllers to be presented are subclasses of a subclass of UIViewController:

class AViewController: UIViewController {}
class BViewController: AViewController {}
class CViewController: AViewController {}
class DViewController: AViewController {}
// ... etc.

I've tried storing the array of AViewController subclasses using the following:

private var array: [AViewController] = [BViewController.self, CViewController.self, DViewController.self]

but I get the error Cannot convert value of type '[BViewController].Type' to specified type '[AViewController]' for the first element.

I will then present BViewController (for instance) using the following:

let ViewController = array[button.tag].self
var viewController: AViewController
viewController = ViewController.init()
viewController.transitioningDelegate = self
viewController.modalPresentationStyle = .custom
present(viewController, animated: true)

Please let me know if this is the incorrect thought process for doing something like this please and thanks for any help.

3 Answers 3

6

You need instances

private var array: [AViewController] = [BViewController(), CViewController(), DViewController()]

if the vcs are in IB , then you would do

let b = self.storyboard!.instantiateViewController(withIdentifier: "bID") as! BViewController
let c = self.storyboard!.instantiateViewController(withIdentifier: "cID") as! CViewController 
let d = self.storyboard!.instantiateViewController(withIdentifier: "dID") as! DViewController

Then

array = [b,c,d]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your suggestion. Does this not use a a lot of memory as I have ~10 subclasses of AViewController and each is fairly hefty in size? Thanks again.
yes, it's a heavy work , you can make a var to keep track of the current index , then load the vc when you need to without making that array or make it an array of storyboard ids
Thanks. Im writing this programmatically so no storyboards or segues. Is this any more work than using segues? A lot of the UIButtons in the view controllers do get initialised until the viewDidLoad function is called ...
you can make a link on the fly such as an Int switch in a function that takes index and return vc like if 1 return BViewController and so on away to make them created when you need to
0
lazy var VCArray :[UIViewController] = {
    return [VCinst(name: "firstVC"),VCinst(name: "secondVC"), VCinst(name: "thirdVC")]
}()

func VCinst(name:String) -> UIViewController {
       return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: name)
}

1 Comment

Hope It will solve issue but please add explaination of your code with it so user will get perfect understanding which he/she really wants.
0

Alternate answer if you want to store array of view controllers as a constant.

 struct AppConstant {

       static let yourViewControllers: [AnyClass] = [AViewController.self, 
            BViewController.self, CViewController.self]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.