iOS Developer
iOS Developer

Reputation: 321

Cannot convert value of type 'ViewController.Type' to expected argument type 'UIViewController'

I have analyzed This question already but it is about alertController What actually I am doing is that, I am working with ContainerViews to a MasterVC. I am following this Tutorial. When I am coming to updateView() function and adding or removing the controllers on these lines I am facing this error.

Cannot convert value of type 'ViewController.Type' to expected argument 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()
}
}

Upvotes: 0

Views: 6239

Answers (2)

Mike Taverne
Mike Taverne

Reputation: 9362

You are attempt to remove a view controller type, instead of an instance of a view controller. Consider this line:

remove(asChildViewController: InstockViewController)

InstockViewController is a type of view controller. You need to pass an instance of that type.

EDIT: In the tutorial you referenced, he explains this in the section "Adding a Child View Controller". There he shows how the master view controller creates instances of the view controllers to be contained.

Upvotes: 0

Yury Bogdanov
Yury Bogdanov

Reputation: 427

This is because your functions (e.g. add(asChildViewController:) ) take UIViewController instances and you are trying to pass types instead.

In other words you are telling "Add CheckoutViewController" not telling specifically which one CheckoutViewController should be added.

You have to create view controller before adding it somewhere. For example, if you use storyboards:

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

For deletion I suggest to see this answer: How to remove the ChildViewController from Parent View Controller in Swift 3

Upvotes: 1

Related Questions