Search code examples
swiftuisplitviewcontroller

Show Image on UISplitViewController


I'm a newbie in SWIFT and I do some examples application.

The only thing that I can not get worked is to get my Image displayed on Detail Scene (DetailViewController).

On my MasterViewController file I have this to get my multiData file:

...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showData" {

        if let indexPath = self.tableView.indexPathForSelectedRow {

            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController

            controller.detailItem = self.multiData[indexPath.section][indexPath.row]
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

// Here I get my multiData
func createData() {

    var first: [Dictionary<String,String>] = []
    var second: [Dictionary<String,String>] = []

    dataSections = ["First Data", "Second Data"]

    first.append(["name": "someName", "image": "somePngFile", "someData": "someText"])        
    second.append(["name": "someName", "image": "somePngFile", "someData": "someText"])

    multiData = [first, second]
}

DetailViewController:

...

@IBOutlet weak var label: UILabel!    
@IBOutlet weak var text: UITextView!
@IBOutlet weak var image: UIImageView!

...

func configureView() {        

    if let detail = self.detailItem {

        if let labelTitle = label {

            labelTitle.text = detail["name"] as! String!
        }

        if let textData = text {

            textData.text = detail["someData"] as! String!
        }

        // This obvious doesn't work
        if let imageFile = image {

            imageFile.image = detail["image"] as! UIImage!
        }
    }
}

So, my question is how can I get the Image from detail["image"]?


Solution

  • I assume that the image contained in detail["image"] is in your application Bundle (i.e.: not an image from an HTTP URL). So, you should use the init(named:) constructor from UIImage to load your image. Something like that:

    if let myImage = UIImage(named: detail["image"]) {
        myImageView.image = myImage
    }
    

    Edit:

    Here is, also, your code with a better management of optionals and casts in configureView:

    func configureView() {
        if let detail = self.detailItem {
    
            if let labelTitle = detail["name"] as? String {
                myLabel.text = labelTitle
            }
    
            if let textViewContent = detail["someData"] as? String {
                myTextView.text = textViewContent
            }
    
            if let myImageName = detail["image"] as? String {
                if let myImage = UIImage(named: myImageName) {
                    myImageView.image = myImage
                }
            }
        }
    }