-1

I am extremely new at Swift and still figuring out the Swift/Objective-C syntax and libraries. I am trying to recreate a simple list of pictures I can view on a different image view. Below is the code and the error message, could someone please guide me in the right direction.

import UIKit

class MasterViewController: UITableViewController {

    var detailViewController: DetailViewController? = nil
    var objects = [AnyObject]()


    override func awakeFromNib() {
        super.awakeFromNib()
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            self.clearsSelectionOnViewWillAppear = false
            self.preferredContentSize = CGSize(width: 320.0, height: 600.0)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let fm = NSFileManager.defaultManager()
        let path = NSBundle.mainBundle().resourcePath!

        let items = fm.contentsOfDirectoryAtPath(path, error: nil)

        for item in items as! [String] {
            if item.hasPrefix("IMG_") {
                objects.append(item)
            }
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Segues

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow() {

                let detailViewController = segue.destinationViewController as! DetailViewController
                detailViewController.detailItem = objects[indexPath.row] as? String
            }
        }
    }

    // MARK: - Table View

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        let object: (AnyObject) = objects[indexPath.row]
        cell.textLabel!.text = object as? String
        return cell
    }

}

I am getting the error in this block of code specifically:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow() {

                let detailViewController = segue.destinationViewController as! DetailViewController
                detailViewController.detailItem = objects[indexPath.row] as? String
            }
        }
    }

The error message in the console comes up as:

Could not cast value of type 'UINavigationController' (0x10a979698) to 'Project1.DetailViewController' (0x1090f27a0).

nhgrif
  • 61,578
  • 25
  • 134
  • 173
itzmurd4
  • 663
  • 10
  • 25

2 Answers2

1

Replace this:

let detailViewController = segue.destinationViewController as! DetailViewController

With this:

let nav = segue.destinationViewController as UINavigationController
let detailViewController = nav.topViewController as! DetailViewController

Solution modified from: DestinationViewController Segue and UINavigationController swift

Community
  • 1
  • 1
Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
  • Rather than copying someone else's answer, the appropriate action here would be to mark the question as a duplicate. – nhgrif Jun 15 '15 at 01:08
0

What makes you think sender is the destination view controller? It's not.

What you want is:

if let detailViewController = segue.destinationViewController as? DetailViewController {

But @nhgrif has a point: you are segue'ing to a nav controller.

Rob
  • 11,446
  • 7
  • 39
  • 57
  • This stops the crash, but still doesn't provide desired results. `detailViewController` would be `nil` and this `if` wouldn't enter. – nhgrif Jun 15 '15 at 01:07
  • Why would it be nil? You downvote people for nothing and provide no details. Perfect. – Rob Jun 15 '15 at 01:12
  • If the segue was created on the storyboard it should have the destination controller automatically. – Rob Jun 15 '15 at 01:12
  • Did you read the error message? The `destinationViewController` is a `UINavigationController`. Your answer still tries casting it as something else. The cast will fail. – nhgrif Jun 15 '15 at 01:19