0

I have the following code which is intended to change views. This code is executed when my 'UITableViewRowAction' is selected and is intended to navigate to a different UITableViewController. However, I am getting the error below and I do not understand what I am doing incorrectly.

 let storyBoard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
 let selectFolderViewController = storyBoard.instantiateViewController(withIdentifier: "SaveArticleTableViewController") as! SaveArticleTableViewController
 self.navigationController?.pushViewController(selectFolderViewController, animated: true)

The error that i am getting in the console is:

 Could not cast value of type 'UINavigationController' (0x106f5c898) to 'lifesci_PubMed.SaveArticleTableViewController' (0x104dbb658).

All my ViewControllers are embedded within a Navigation Controller

brandonscript
  • 57,554
  • 29
  • 142
  • 204
jamesMcKey
  • 353
  • 3
  • 20
  • I would encourage you to call the class simply **"SelectArticle"**. Whatever you call the Class, you **>>>MUST<< – Fattie Oct 22 '17 at 00:07
  • I would encourage you to simply **not use** navigation controllers. They are kind of .. silly. It really just causes problems when you're learning the basics. – Fattie Oct 22 '17 at 00:09

2 Answers2

1

It's very likely that you have not properly set the custom class of the view controller in Storyboard. Here's an image ...

enter image description here

You would type "SaveArticleTableViewController" in that slot.

It's quite possible you have other problem as well, but that will get you going.

Fattie
  • 30,632
  • 54
  • 336
  • 607
0

The error is actually quite descriptive here:

Could not cast value of type 'UINavigationController' to 'SaveArticleTableViewController'

At first I thought that this was because your view controller SaveArticleTableViewController is not actually set with a custom class. If you're using Interface Builder, make sure you've set the class type in the view controller's property inspector:

If you're doing it in code, make sure your custom class is inheriting properly.

But taking a leap of a guess, your target is "UINavigationController", but your custom class name that it's trying to cast to suggests that your target is a table view "SaveArticleTableViewController". Moreover, I'd bet that this is the tableview that is the source not the destination. I'd double check that you're actually instantiating the right view controller - typically you'd name this something like "SaveArticleDetailViewController", or guessing by your code, maybe it's "SelectFolderViewController", but that's up to you.

For example:

let storyBoard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let selectFolderViewController = storyBoard.instantiateViewController(withIdentifier: "SelectFolderViewController") as! SelectFolderViewController
self.navigationController?.pushViewController(selectFolderViewController, animated: true)

This also requires that you set self.navigationController to the containing nav controller.

brandonscript
  • 57,554
  • 29
  • 142
  • 204
  • Thank you. You are correct. As a side question. Since i am using UITableViewRowAction and the function 'func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?' to move to another ViewController, I can't figure out how to transfer data from the initial table row to the new viewController - as I cannot really use Segues in this scenario - at least I do not believe I can. Would you be able to advise? – jamesMcKey Oct 22 '17 at 00:09
  • 1
    No, you must ask a new separate question. – Fattie Oct 22 '17 at 00:10
  • Fwiw, there are a ton of answers on SO already for that. https://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift – brandonscript Oct 22 '17 at 00:13
  • You can easily use a segue; just send the appropriate object as the `sender` – Paulw11 Oct 22 '17 at 00:15