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

时间:2017-03-19 02:44:07

标签: swift uiviewcontroller segue

我正在尝试从viewcontroller管理viewcontroller传递一个boolean语句到另一个使用segue,我得到“从uiviewcontroller转换为无关类型总是失败”错误。

mainVC

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{

我收到错误。

2 个答案:

答案 0 :(得分:1)

您需要输入广告sender以使Boolean值不是segue.destination,因为目标的类型为UIViewController

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

如果您想将此Bool值传递给下一个控制器,请输入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
    }
}

答案 1 :(得分:0)

在您的代码中segue.destination将成为UIViewController。那你为什么要把它投到Bool?是演员表给你一个错误。你可能在那里看错了值。解决这个问题你应该没问题。)