简体   繁体   English

iOS (Swift) - UIViewControllers 数组

[英]iOS (Swift) - Array of UIViewControllers

I have an array UIButton s that have a unique tag value.我有一个具有唯一tag值的数组UIButton When a given button is pressed, I want to load and present a UIViewController that is also stored in an array of equal length.当按下给定的按钮时,我想加载并呈现一个UIViewController ,它也存储在一个等长的数组中。

The UIViewController s to be presented are subclasses of a subclass of UIViewController :将要介绍的UIViewControllerUIViewController子类的子类:

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:我尝试使用以下方法存储AViewController子类的数组:

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.但我收到错误无法将类型“[BViewController].Type”的值转换为第一个元素的指定类型“[AViewController]”

I will then present BViewController (for instance) using the following:然后,我将使用以下内容呈现BViewController (例如):

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.请让我知道这是否是执行此类操作的错误思考过程,并感谢您的帮助。

I have an array UIButton s that have a unique tag value.我有一个具有唯一tag值的数组UIButton When a given button is pressed, I want to load and present a UIViewController that is also stored in an array of equal length.当按下给定的按钮时,我想加载并呈现一个UIViewController ,它也存储在一个等长的数组中。

The UIViewController s to be presented are subclasses of a subclass of UIViewController :UIViewController s到呈现的子类的子类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:我尝试使用以下方法存储AViewController子类的数组:

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.但我收到错误无法将类型 '[BViewController].Type' 的值转换为第一个元素的指定类型 '[AViewController]'

I will then present BViewController (for instance) using the following:然后我将使用以下内容呈现BViewController (例如):

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.请让我知道这是否是执行此类操作的错误思考过程,并感谢您的帮助。

I have an array UIButton s that have a unique tag value.我有一个具有唯一tag值的数组UIButton When a given button is pressed, I want to load and present a UIViewController that is also stored in an array of equal length.当按下给定的按钮时,我想加载并呈现一个UIViewController ,它也存储在一个等长的数组中。

The UIViewController s to be presented are subclasses of a subclass of UIViewController :UIViewController s到呈现的子类的子类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:我尝试使用以下方法存储AViewController子类的数组:

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.但我收到错误无法将类型 '[BViewController].Type' 的值转换为第一个元素的指定类型 '[AViewController]'

I will then present BViewController (for instance) using the following:然后我将使用以下内容呈现BViewController (例如):

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.请让我知道这是否是执行此类操作的错误思考过程,并感谢您的帮助。

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]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM