iOS (Swift) - UIViewController 数组
iOS (Swift) - Array of UIViewControllers
我有一个数组 UIButton
具有唯一的 tag
值。当按下给定的按钮时,我想加载并显示一个 UIViewController
,它也存储在一个等长的数组中。
要呈现的 UIViewController
是 UIViewController
子类的子类:
class AViewController: UIViewController {}
class BViewController: AViewController {}
class CViewController: AViewController {}
class DViewController: AViewController {}
// ... etc.
我尝试使用以下方法存储 AViewController
个子类的数组:
private var array: [AViewController] = [BViewController.self, CViewController.self, DViewController.self]
但我得到错误无法将类型“[BViewController]”的值转换为第一个元素的指定类型“[AViewController]”。
然后我将使用以下内容呈现 BViewController
(例如):
let ViewController = array[button.tag].self
var viewController: AViewController
viewController = ViewController.init()
viewController.transitioningDelegate = self
viewController.modalPresentationStyle = .custom
present(viewController, animated: true)
请告诉我这是否是执行此类操作的错误思考过程,感谢您的帮助。
您需要实例
private var array: [AViewController] = [BViewController(), CViewController(), DViewController()]
如果 vcs 在 IB 中,那么你会做
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
然后
array = [b,c,d]
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)
}
我有一个数组 UIButton
具有唯一的 tag
值。当按下给定的按钮时,我想加载并显示一个 UIViewController
,它也存储在一个等长的数组中。
要呈现的 UIViewController
是 UIViewController
子类的子类:
class AViewController: UIViewController {}
class BViewController: AViewController {}
class CViewController: AViewController {}
class DViewController: AViewController {}
// ... etc.
我尝试使用以下方法存储 AViewController
个子类的数组:
private var array: [AViewController] = [BViewController.self, CViewController.self, DViewController.self]
但我得到错误无法将类型“[BViewController]”的值转换为第一个元素的指定类型“[AViewController]”。
然后我将使用以下内容呈现 BViewController
(例如):
let ViewController = array[button.tag].self
var viewController: AViewController
viewController = ViewController.init()
viewController.transitioningDelegate = self
viewController.modalPresentationStyle = .custom
present(viewController, animated: true)
请告诉我这是否是执行此类操作的错误思考过程,感谢您的帮助。
您需要实例
private var array: [AViewController] = [BViewController(), CViewController(), DViewController()]
如果 vcs 在 IB 中,那么你会做
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
然后
array = [b,c,d]
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)
}