denis_lor
denis_lor

Reputation: 6547

Instantiate multiple viewcontroller with same global variables

How do I declare a viewcontroller from a list of viewcontroller that have some variable similar without repeating myself and without making a base viewcontroller? Something like:

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)

swtich(type){
  case "A":
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeAViewController") as! TypeAViewController
    break;
  case "B":
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeBViewController") as! TypeBViewController
    break;
}
vcGeneric.variableSame1 = "SomeValue1"
vcGeneric.variableSame2 = "SomeValue2"
vcGeneric.variableSame3 = "SomeValue3"
self.present(vcGeneric, animated: true, completion: nil)

I tried by declaring var vcGeneric: UIViewController but I receive a compile error Value of type "UIViewController" has no member "variableSame1"

Upvotes: 0

Views: 401

Answers (2)

Aashish
Aashish

Reputation: 2696

You can use inheritance method to achieve it:

Suppose if we have to set some value to specific view controllers only, we also can achieve it without any hassle.

/// Common view controller parent class which stores common or non-common properties that needs to be accessed globally
class CommonViewController: UIViewController {
    var variableSame1: String?
    var variableSame2: String?
    var variableDiff1: String?
    var variableDiff2: Int?
}

class ViewController1: CommonViewController {
    var variableSame1: String?
    var variableSame2: String?
}
class ViewController2: CommonViewController {
    var variableSame1: String?
    var variableSame2: String?
    var variableDiff1: String?
}
class ViewController3: CommonViewController {
    var variableSame1: String?
    var variableSame2: String?
    var variableDiff2: Int?
}

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
var type = "A"
var vcGeneric: CommonViewController!
switch type {
case "A":
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeAViewController") as! ViewController1
    break;
case "B":
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeBViewController") as! ViewController2
    vcGeneric.variableDiff1 = "Different value 1"
    break;
default:
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeCViewController") as! ViewController3
    vcGeneric.variableDiff2 = 458
    break;
}
vcGeneric.variableSame1 = "Some value 1"
vcGeneric.variableSame2 = "Some value 2"

self.present(vcGeneric, animated: true, completion: nil)

It is matter of preference, we can use either protocol as @Andy Obusek has answered above precisely, or we can use inheritance method as above.

Upvotes: 0

Andy Obusek
Andy Obusek

Reputation: 12842

Use a protocol to define the common properties, like this:

protocol CommonViewController {
    var variableSame1: String? { get set }
    var variableSame2: String? { get set }
    var variableSame3: String? { get set }
}

class ViewController1: UIViewController, CommonViewController {
    var variableSame1: String?
    var variableSame2: String?
    var variableSame3: String?
}
class ViewController2: UIViewController, CommonViewController {
    var variableSame1: String?
    var variableSame2: String?
    var variableSame3: String?
}
class ViewController3: UIViewController, CommonViewController {
    var variableSame1: String?
    var variableSame2: String?
    var variableSame3: String?
}

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
var type = "A"
var vcGeneric: CommonViewController?
switch type {
case "A":
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeAViewController") as! ViewController1
    break;
case "B":
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeBViewController") as! ViewController2
    break;
default:
    vcGeneric = storyboard.instantiateViewController(withIdentifier: "TypeCViewController") as! ViewController3
    break;
}
vcGeneric.variableSame1 = "SomeValue1"
vcGeneric.variableSame2 = "SomeValue2"
vcGeneric.variableSame3 = "SomeValue3"
self.present(vcGeneric, animated: true, completion: nil)

Upvotes: 4

Related Questions