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

问题描述

情况:当我按下rentViewController中的一个按钮时,它会弹出一个tableviewcontroller.如果按下了特定单元格,它会将数据发送到rentViewController.为了将数据从一个视图控制器发送到另一个我需要代码

The situation: when I press a button in rentViewController, it pops up a tableviewcontroller. If a specific cell has been pressed, it sends data to rentViewController. In order to send data from one view controller to another I needed the code

letrentViewController : RentViewController = self.presentingViewController as!RentViewController <- 这里是错误出现的地方

let rentViewController : RentViewController = self.presentingViewController as! RentViewController <- here is where the error shows up

这样tableviewcontroller 就可以访问rentviewcontroller 的变量和函数.我正在使用

so that tableviewcontroller could get access to the variables and functions from rentviewcontroller. I'm using

self.dismiss(animated: true, completion: nil)

离开tableviewcontroller,回到rentviewcontroller.但是,它给了我一个错误无法将‘UITabBarController’类型的值转换为‘RentViewController’".我做了一些研究,我认为这是根据我的视图控制器的命令,但我不确定如何以一种有效的方式改变它.我的初始视图是TabBarController",之后的顺序是NavigationController"->RentViewController"->TableViewController".如果您有任何问题,请随时提出,我可以为您提供更多信息.

to get out of the tableviewcontroller and back to the rentviewcontroller. However, it gives me an error "Could not cast value of type 'UITabBarController' to 'RentViewController'". I did some research and I think it's according to the orders of my view controllers but I'm not sure how to change it in a way that it works. My initial view is 'TabBarController' and the order after that is 'NavigationController' -> 'RentViewController' -> 'TableViewController'. If you have questions feel free to ask I can provide you more information.

推荐答案

您的 viewController 正在从 UITabBarController 呈现.使用您正在使用的方法,我相信您可以像这样访问它(其中 index 是包含 RentVC 的 UINavigationController 的 UITabBarController 中的索引):

Your viewController is being presented from UITabBarController. With approach you are using I believe you can access it like this (where index is index in UITabBarController of your UINavigationController containing RentVC):

if let tab = self.presentingViewController as? UITabBarController,
    let nav = tab.viewControllers?[index] as? UINavigationController,
    let rentViewController = nav.viewControllers.first as? RentViewController {

        rentViewController.data = data
}

但是,我建议在这种情况下使用委托或回调块来传递数据.

However, I would suggest using a delegate or callback block to pass data in this occassion.

对于委托方法,首先创建协议:

For delegate approach, first create protocol:

protocol PassDataDelegate:class {
    func passData(data:YourType)
}

然后在 TableViewController 中:

Then in TableViewController:

class TableViewController: UIViewController {
    weak var delegate: PassDataDelegate?
}

在 RentViewController 中:

And in RentViewController:

extension RentViewController: PassDataDelegate {
    func passData(data:YourType) {
        //use data to suit your needs
    }
}

在呈现 TableViewController 之前,在 RentViewController 中设置它的委托:

Before presenting TableViewController, in RentViewController, set its delegate:

tableViewController.delegate
present(tableViewController, animated: true)

最后,在 TableViewController 内部,在关闭之前,调用委托的方法来传递数据:

And finally, inside TableViewController, before dismissing, call delegate's method to pass data:

delegate?.passData(data: <<someData>>)

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

06-28 02:52