无法将'ViewController.Type'类型的值转换为预期的参数类型'UIViewController'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将'ViewController.Type'类型的值转换为预期的参数类型'UIViewController'相关的知识,希望对你有一定的参考价值。

我已经分析了This问题,但它是关于alertController实际上我正在做的是,我正在与ContainerViews工作到MasterVC。我正在关注这个Tutorial。当我来到updateView()功能并添加或删除这些线路上的控制器时,我遇到了这个错误。

无法将'ViewController.Type'类型的值转换为预期的参数类型'UIViewController'

enter image description here

import UIKit

class MasterViewController: UIViewController {

@IBAction func segmentcontrol(_ sender: Any) {
}

@IBOutlet weak var segmentedCont: UISegmentedControl!
@IBOutlet weak var cancelMaster: UIBarButtonItem!

@IBAction func cancelMasterAct(_ sender: Any) {
}

@IBOutlet weak var createMaster: UIBarButtonItem!

@IBAction func createMasterAct(_ sender: Any) {
}


func setupView() {
    setupSegmentedControl()

    updateView()
}
private func setupSegmentedControl() {

   //  Configure Segmented Control

    segmentedCont.removeAllSegments()
    segmentedCont.insertSegment(withTitle: "Instock", at: 0, animated: false)
    segmentedCont.insertSegment(withTitle: "Checkin", at: 1, animated: false)
    segmentedCont.insertSegment(withTitle: "Inspection", at: 2, animated: false)
    segmentedCont.insertSegment(withTitle: "Checkout", at: 3, animated: false)
    segmentedCont.addTarget(self, action: #selector(selectionDidChange(_:)), for: .valueChanged)
    // Select First Segment
    segmentedCont.selectedSegmentIndex = 0


}

@objc func selectionDidChange(_ sender: UISegmentedControl) {
    updateView()
}

private func updateView() {


    if segmentedCont.selectedSegmentIndex == 0 {
        remove(asChildViewController: CheckinViewController)
        remove(asChildViewController: CheckoutViewController)
        remove(asChildViewController: InspectionViewController)
        add(asChildViewController: InstockViewController)
    }
    if segmentedCont.selectedSegmentIndex == 1 {
        remove(asChildViewController: InstockViewController)
        remove(asChildViewController: CheckinViewController)
        remove(asChildViewController: InspectionViewController)
        add(asChildViewController: CheckinViewController)
    }
    if segmentedCont.selectedSegmentIndex == 2 {
        remove(asChildViewController: InstockViewController)
        remove(asChildViewController: CheckoutViewController)
        remove(asChildViewController: CheckinViewController)
        add(asChildViewController: InspectionViewController)

    }

    else {
            remove(asChildViewController: InstockViewController)
            remove(asChildViewController: CheckinViewController)
            remove(asChildViewController: InspectionViewController)
            add(asChildViewController: CheckoutViewController)
    }

}


private func add(asChildViewController viewController: UIViewController) {
    // Add Child View Controller
    addChildViewController(viewController)

    // Add Child View as Subview
    view.addSubview(viewController.view)

    // Configure Child View
    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Notify Child View Controller
    viewController.didMove(toParentViewController: self)
}

private lazy var CheckinViewController : CheckinViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "CheckinViewController") as! CheckinViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

private lazy var  InstcokViewController : InstockViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name : "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "InstockViewController") as! InstockViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()



private func remove(asChildViewController viewController: UIViewController) {
    // Notify Child View Controller
    viewController.willMove(toParentViewController: nil)

    // Remove Child View From Superview
    viewController.view.removeFromSuperview()

    // Notify Child View Controller
    viewController.removeFromParentViewController()
}

override func viewDidLoad() {

    super.viewDidLoad()
    self.setupView()
    print("MasterVC Printed")
    self.view.addSubview(segmentedCont)
    navigationController?.navigationBar.barTintColor = UIColor(red:0.00, green:0.52, blue:1.00, alpha:1.0)
    navigationController?.navigationBar.tintColor = UIColor.white

    segmentedCont.tintColor = UIColor.black

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}
答案

这是因为你的函数(例如add(asChildViewController :))接受UIViewController实例,而你试图传递类型。

换句话说,你告诉“Add CheckoutViewController”没有具体说明应该添加哪一个CheckoutViewController。

在将其添加到某处之前,您必须创建视图控制器。例如,如果您使用故事板:

if segmentedCont.selectedSegmentIndex == 0 {
    guard let instockVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Instock") as? InstockViewController else { return }
    add(asChildViewController: instockVC)
}

删除我建议看到这个答案:How to remove the ChildViewController from Parent View Controller in Swift 3

另一答案

您尝试删除视图控制器类型,而不是视图控制器的实例。考虑这一行:

remove(asChildViewController: InstockViewController)

InstockViewController是一种视图控制器。您需要传递该类型的实例。

编辑:在您引用的教程中,他在“添加子视图控制器”一节中解释了这一点。在那里,他展示了主视图控制器如何创建要包含的视图控制器的实例。

以上是关于无法将'ViewController.Type'类型的值转换为预期的参数类型'UIViewController'的主要内容,如果未能解决你的问题,请参考以下文章

无法将'CGFloat'类型的值转换为闭包结果类型'CGPoint'

TypeError:无法将属性'innertext'设置为null

TypeError:无法将值''视为常量,因为它具有未知的类型'function'

无法将'ViewController.Type'类型的值转换为预期的参数类型'UIViewController'

无法将关键字'user'解析为字段-Django错误

ValueError:无法将字符串转换为float:'“”'