gpt4 book ai didi

Swift 3 - 从 uiviewcontroller 到不相关类型的 Segue/Cast 总是失败

转载 作者:行者123 更新时间:2023-11-28 15:46:56 25 4
gpt4 key购买 nike

我正在尝试使用 segue 将 bool 语句从管理 View Controller 的 View Controller 传递到另一个 View Controller ,但我收到“从 uiviewcontroller 转换为不相关类型总是失败”的错误。

主VC

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = theViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1

if(viewControllerIndex == 0){
let sessionTimerActive: Bool = false
performSegue(withIdentifier: "sessionActiveBool", sender: sessionTimerActive)

}

secondVC

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let sessionTrackerBoolClone = segue.destination as? Bool{
sesssionTrackerActive = sessionTrackerBoolClone
}
}

然而在行

if let sessionTrackerBoolClone = segue.destination as? Bool{

我遇到了错误。

最佳答案

您需要输入 cast sender 以获得您的 Boolean 值而不是 segue.destination 因为 destination 是 UIViewController< 的类型.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let sessionTrackerBoolClone = sender as? Bool{
sesssionTrackerActive = sessionTrackerBoolClone
}
}

如果您想将此 Bool 值传递给您的下一个 Controller ,请将 destination 类型转换为您的 ViewController 并设置其属性。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! ViewController //Your ViewController class
if let sessionTrackerBoolClone = sender as? Bool{
vc.sesssionTrackerActive = sessionTrackerBoolClone
}
}

关于Swift 3 - 从 uiviewcontroller 到不相关类型的 Segue/Cast 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42882345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com