UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED Project 1 - display text when no image selected

Forums > macOS

I have completed Project 1 - StormViewer - but I want to add a small enhancement. I would like to display a text such as "Please select an image to display" on the DetailView if there is no image selected in the SourceView - at start up for example or if someone deselects the image.

Thanks for any suggestions,

ScottyNL

3      

Found the answer myself. Add an NSTextField to the DetailView and link it to the DetailViewController as an IBOutlet called commentView use the isHidden attribute of commentView to hide it when an image is selected and the isHidden attribute of imageView to unhide it (the attribute is initially set to true in the storyboard)

class DetailViewController: NSViewController {
    @IBOutlet var imageView: NSImageView!
    @IBOutlet var commentView: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do view setup here.
    }

    func imageSelected(name: String) {
        commentView.isHidden = true
        imageView.isHidden = false
        imageView.image = NSImage(named: name)
    }

    func noImageSelected() {
        commentView.isHidden = false
        imageView.isHidden = true
    }
}

And "unselect" an image is of course Command-Click - and then the text view reappears as a result of noImageSelected.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.