使用相同的全局变量实例化多个 viewcontroller
Instantiate multiple viewcontroller with same global variables
我如何从 viewcontroller 的列表中声明一个 viewcontroller,它有一些类似于 的变量而不重复我自己 和 没有做基础 viewcontroller?类似于:
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)
我尝试通过声明 var vcGeneric: UIViewController
但我收到一个编译错误 Value of type "UIViewController" has no member "variableSame1"
使用协议定义公共属性,如下所示:
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)
我如何从 viewcontroller 的列表中声明一个 viewcontroller,它有一些类似于 的变量而不重复我自己 和 没有做基础 viewcontroller?类似于:
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)
我尝试通过声明 var vcGeneric: UIViewController
但我收到一个编译错误 Value of type "UIViewController" has no member "variableSame1"
使用协议定义公共属性,如下所示:
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)