无法转换“UINavigationController"类型的值 [英] Could not cast value of type 'UINavigationController'

查看:27
本文介绍了无法转换“UINavigationController"类型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing a search interface for my application, so basically I will pass the search keyword from one ViewController to another 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.

现在代码看起来像这样.

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;
    }
}

我已经探索过这些问题DestinationViewController Segue 和 UINavigationController swift, 当我的 ViewController 嵌入到 UINavigationController 中时,我如何转接值?没有运气.

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.

让我感到奇怪的是,我已经在导航控制器中嵌入了 ViewControllers,我可以通过 segueing 来传递参数.但是,在这种情况下,它会引发 Could not cast value of type 'UINavigationController' 错误.如果我尝试上面注释的代码,它不会在那里抛出错误,而是在 AppDelegate 类中抛出错误.

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 AppDelegate class.

推荐答案

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

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;

这篇关于无法转换“UINavigationController"类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆