【发布时间】:2016-06-21 06:24:36
【问题描述】:
我正在尝试从模型类访问枚举以编写 switch case 来执行 segue。这是我的代码:
class LandingViewController: UIViewController {
// MARK: Private Structs.
private struct SegueIdentifier {
static let forcedUpdate = "forcedUpdate"
static let optionalUpdate = "optionalUpdate"
}
// MARK: Private variables.
private let updateType: StartupManager.UpdateType
// MARK: LifeCycle Methods.
override func viewDidLoad() {
super.viewDidLoad()
StartupManager.setupForLanding()
var segueIdentifier: String
switch updateType {
case .ForcedUpdate: segueIdentifier = SegueIdentifier.forcedUpdate
case .OptionalUpdate: segueIdentifier = SegueIdentifier.optionalUpdate
}
performSegueWithIdentifier(segueIdentifier, sender: nil)
}
setupForLanding() 用于检查 startUpManager 模型类以查看哪个枚举被触发。
class StartupManager: NSObject {
enum UpdateType {
case OptionalUpdate
case ForcedUpdate
}
// code to perform a check
if isForceUpdate {
completion(.ForcedUpdate)
} else {
completion(.OptionalUpdate)
}
但我不断收到错误消息LandingViewController has no initialisers。如何在启动管理器中检查调用了哪个案例,然后在landingViewController中执行segue?
【问题讨论】:
-
正如 Thien Liu 指出的,问题在于
updateType没有默认类型。要么将其默认为某个值(或使其成为可选值)。但是,顺便说一句,您可能不想在viewDidLoad中执行转场。您可能希望将其推迟到流程的后期,例如viewDidAppear。根据您转换到此场景的方式,您可能会收到有关在另一个转换中间启动转换的错误。
标签: ios swift enums swift2 segue