Xcode 6中使用prepare for segue实现多个segue的转场(Swift)

4

我目前在开发一个应用程序,有一个主要的ViewController和2个其他的ViewControllers
其中一个是我的浏览器,它运行良好,另一个是一个TableView
我有5个Buttons,它们可以将我带到浏览器,还有一个应该可以进入我的TableView
我使用Present Modally segueButton连接到我的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连接到另一个视图控制器或使用其他按钮,它就不会像这样正常工作。

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

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"


    }



}

Thanks in advance


你能指明哪个标识符对应哪个视图控制器吗? - asifmohd
你确定你正在使用导航控制器吗? - Dani
每个segue都可以导航到我的浏览器,只有那个没有标识符/没有代码的按钮无法导航到一个导航控制器。 - gabri sondi
4个回答

2
错误日志提到了以下内容:
Swift动态类型转换失败
这意味着问题出现在swift尝试将segue.destinationViewController转换为WebViewController时。
只有当destinationViewController不是WebViewController时才会发生这种情况。
这就是当应用程序试图转换到您提到的tableViewController时所发生的事情。
因此,要修复此错误,您必须在每个if条件内初始化DestViewController,或检查segue的标识符是否不是tableViewController的标识符,然后初始化和转换destinationViewController。

1

可能导航控制器是目标,所以您需要像这样做:

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

1
这个segue/dynamic cast failed问题似乎经常出现,但很容易解决。
如果您的webview控制器嵌入在UINavigationController中,则segue.destinationViewControllerUINavigationController实例,而不是DestViewController实例。
您可能可以保留导航控制器并替换此行: var DestViewConroller : WebViewController = segue.destinationViewController as WebViewController 用这个: var DestViewConroller : WebViewController = segue.destinationViewController.topViewController as WebViewController UINavigationController.topViewController属性指的是导航堆栈的顶部,应该是您的DestViewConroller

0

问题似乎出现在对象转换中。 请确保您已经将DestViewConroller类设置为Storyboard中的WebViewController。如果没有这样做,尽管强制转换,DestViewConroller仍会被视为UIViewController的实例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接