xcode 无法转换“UINavigationController”类型的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30209626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:08:04  来源:igfitidea点击:

Could not cast value of type 'UINavigationController'

xcodeswift

提问by Semih Yagcioglu

I am implementing a search interface for my application, so basically I will pass the search keyword from one ViewController to another ViewController.

我正在为我的应用程序实现一个搜索界面,所以基本上我会将搜索关键字从一个 ViewController 传递到另一个 ViewController。

I have done this type of parameter passing several times but something seems strange this time. The destination ViewController is embedded in a Navigation Controller.

我已经多次传递这种类型的参数,但这次似乎有些奇怪。目标 ViewController 嵌入在导航控制器中。

Now the code looks something like this.

现在代码看起来像这样。

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject!) {
    if (segue?.identifier == "home_to_search") {

       var searchKeyword = txtSearchBarHome.text
       var svc = segue?.destinationViewController as! SearchViewController;
       svc.toPassSearchKeyword = searchKeyword;

       //let nav = segue?.destinationViewController as! UINavigationController
       //let svc = nav.topViewController as! SearchViewController
       //svc.toPassSearchKeyword = searchKeyword;
    }
}

I have already explored these questions DestinationViewController Segue and UINavigationController swift, How do I segue values when my ViewController is embedded in an UINavigationController?with no luck.

我已经探讨了这些问题 DestinationViewController Segue 和 UINavigationController swift当我的 ViewController 嵌入到 UINavigationController 中时,我如何转义值?没有运气。

What makes me wonder is that I already have ViewControllers embedded in Navigation Controllers that I can pass parameters by segueing. However, in this case it throws a Could not cast value of type 'UINavigationController'error. If I try the commented code above it does not throw an error there, but at the AppDelegateclass.

让我感到奇怪的是,我已经在导航控制器中嵌入了 ViewControllers,我可以通过 segueing 传递参数。但是,在这种情况下,它会引发“UINavigationController”类型无法转换值错误。如果我尝试上面的注释代码,它不会在那里抛出错误,而是在AppDelegate类中抛出错误。

回答by Semih Yagcioglu

Answering my own question. In this case we need to access the child view by doing something like this:

回答我自己的问题。在这种情况下,我们需要通过执行以下操作来访问子视图:

let nav = segue?.destinationViewController as! UINavigationController
let svc = nav.topViewController as! SearchViewController
svc.toPassSearchKeyword = searchKeyword;

回答by Json

Based on Semih's answer, you can also do this to pass a variable to next segue if you don't want to use an identifier:

根据 Semih 的回答,如果您不想使用标识符,也可以这样做以将变量传递给下一个 segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let nav = segue.destination as? UINavigationController, 
        let vc = nav.topViewController as? TestViewController {
        vc.username = "Test"
    }
}

回答by Cornelis Kuijpers

Just a quick note for those looking into this issue it has been renamed to Destination

只是给那些调查这个问题的人一个简短的说明,它已被重命名为 Destination

let nav = segue.destination as! UINavigationController
let svc = nav.topViewController as! SearchViewController
svc.toPassSearchKeyword = searchKeyword;