Xcode 6 swift 多个 segues 准备 segue

Xcode 6 swift multiple segues with prepare for segue

我目前正在开发一款应用,其中有一个主要 ViewController 和另外 2 个 ViewControllers
一个是我的浏览器,运行良好,另一个是 TableView.
我有 5 个 Buttons 引导我进入我的浏览器,一个应该进入我的 TableView.
我已将 ButtonPresent Modally segue 连接到我的 TableView 控制器(已嵌入 NavigationViewController)。
但是每次我尝试按下按钮时,模拟器都会崩溃并出现此错误:

libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10b9bf860:  pushq  %rbp
0x10b9bf861:  movq   %rsp, %rbp
0x10b9bf864:  testq  %rdi, %rdi
0x10b9bf867:  je     0x10b9bf89e               ; swift_dynamicCastClassUnconditional + 62
0x10b9bf869:  movabsq $-0x7fffffffffffffff, %rax
0x10b9bf873:  testq  %rax, %rdi
0x10b9bf876:  jne    0x10b9bf89e               ; swift_dynamicCastClassUnconditional + 62
0x10b9bf878:  leaq   0xb52e9(%rip), %rax
0x10b9bf87f:  movq   (%rax), %rax
0x10b9bf882:  andq   (%rdi), %rax
0x10b9bf885:  nopw   %cs:(%rax,%rax)
0x10b9bf890:  cmpq   %rsi, %rax
0x10b9bf893:  je     0x10b9bf8ad               ; swift_dynamicCastClassUnconditional + 77
0x10b9bf895:  movq   0x8(%rax), %rax
0x10b9bf899:  testq  %rax, %rax
0x10b9bf89c:  jne    0x10b9bf890               ; swift_dynamicCastClassUnconditional + 48
0x10b9bf89e:  leaq   0x36b7d(%rip), %rax       ; "Swift dynamic cast failed"
0x10b9bf8a5:  movq   %rax, 0xb4c0c(%rip)       ; gCRAnnotations + 8
0x10b9bf8ac:  int3   
0x10b9bf8ad:  movq   %rdi, %rax
0x10b9bf8b0:  popq   %rbp
0x10b9bf8b1:  retq   
0x10b9bf8b2:  nopw   %cs:(%rax,%rax)

如果我将我的 segue 连接到另一个 viewcontroller 或者如果我使用其他按钮,它也不会工作。

我知道问题出在这段代码中,因为如果我删除它,一切都会正常:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {


    var DestViewConroller : WebViewController = segue.destinationViewController as WebViewController



    if (segue.identifier == "homeSegue"){

        DestViewConroller.url = "https://edu.sh.ch"


    }

    if (segue.identifier == "mailSegue"){

        DestViewConroller.url = "https://edumail.sh.ch/owa"

    }

    if (segue.identifier == "mensaSegue"){

        DestViewConroller.url = "http://kanti.sh.ch/fileadmin/Redaktoren/Service/Mensa/Menueplan.pdf"

    }

    if (segue.identifier == "absenzenSegue"){

        DestViewConroller.url = "https://edu.sh.ch/Lists/Absenzen/Heute%20%20Knftige.aspx"

    }

    if (segue.identifier == "stundenplanSegue"){

        DestViewConroller.url = "https://edu.sh.ch/Informationen/Stundenplaene/SiteAssets/SitePages/Homepage/klassen_03_juli.pdf"


    }



}

提前致谢

错误日志提到

Swift dynamic cast failed

所以这意味着当 swift 尝试将 segue.destinationViewController 转换为 WebViewController 时会出现问题。

这只会在 destinationViewController 不是 WebViewController 时发生。

当应用程序尝试转到您提到的 tableViewController 时会发生什么。

因此,要修复此错误,您必须在每个 if 条件中初始化 DestViewController 或检查 segue 的标识符是否不是 tableViewController 的标识符,然后初始化并转换 destinationViewController.

可能导航控制器就是目的地,所以你必须这样做:

var webViewController = (segue.destinationViewController as?
 UINavigationController)?.viewControllers[0] as? webViewController

问题似乎出在对象转换中。 确保您已在 Storyboard 中将 DestViewConroller 的 class 设置为 WebViewController。如果不这样做,DestViewConroller 将被假定为 UIViewController 的一个实例,尽管进行了转换。

这个segue/dynamic转换失败问题似乎经常出现,但很容易修复。

如果您的 webview 控制器嵌入在 UINavigationController 中,那么 segue.destinationViewController 是一个 UINavigationController 实例,而不是 DestViewController 实例。

您可能可以保留导航控制器并替换此行:

var DestViewConroller : WebViewController = segue.destinationViewController as WebViewController

有了这个:

var DestViewConroller : WebViewController = segue.destinationViewController.topViewController as WebViewController

UINavigationController.topViewController属性指的是导航栈的顶部,应该是你的DestViewConroller