Check if image with name is set (WatchKit)

302 views Asked by At

I'd like to check if an image with the name "image.png" is set to the image view "imgView". I know how I can do this with Swift in the normal ViewController.swift but don't know how I can do this in the watch kit extension (InterfaceController.swift). Does someone of you guys know how I can do this?

1

There are 1 answers

0
thealienisreal On BEST ANSWER

Looking at the header for WKInterfaceImage:

@available(watchOS 2.0, *)
class WKInterfaceImage : WKInterfaceObject, WKImageAnimatable {

    func setImage(image: UIImage?)
    func setImageData(imageData: NSData?)
    func setImageNamed(imageName: String?)

    func setTintColor(tintColor: UIColor?)
}

The API only has setters and no getters. One strategy might be to subclass this and track the setting, and write your own custom getters, but unfortunately you can't change to a custom class in the storyboard, so you can't use that custom subclass anyway. You'd also be unable to programmatically read images that were set from the storyboard directly.

But to answer your question, you can't ask WKInterfaceImage what image it has. The only way would be to track what images you're setting manually like this:

@IBOutlet var myImage: WKInterfaceImage!

var lastImageUsed: String? = "image.png" // Pre-populate with the image used for myImage in the storyboard

func setImageWrapper(newImage: String) {
    lastImageUsed = newImage
    myImage.setImageNamed(newImage)
}

func getImageWrapper() -> String? {
    return lastImageUsed
}