watchkitwkinterfacecontroller

Update InterfaceController with different data


Hi I am new to WatchKit development. I want to know that can I update a single interface controller with multiple data for button clicks


Solution

  • You could have many different objects in your InterfaceController, such as labels, image views, etc.

    Updating WKInterfaceLabels

    You should call the setText() method on the corresponding label.

    For example, you have a button and a label, and you want to print "Hello" on the label when the button is clicked. In this case, you should connect an action to the button in your interface (by control-dragging button to the code), and then add the following code in the method created:

    Swift

    label1.setText("Hello")
    

    Objective-C

    [label1 setText:@"Hello"];
    

    Updating WKInterfaceImages

    You should call setImage() or setImageNamed() methods on the corresponding image view.

    First, the image should be located in the Asset Catalog of your WatchKit App Target, must be bundled or available as an UIImage. Then you could use these codes:

    Case #1: Available as file in bundle or Asset Catalog

    Swift

    image1.setImageNamed("imageName")
    

    Objective-C

    [image1 setImageNamed:@"imageName"];
    

    Case #2: Available as an UIImage

    Swift

    image1.setImage(image)
    

    Objective-C

    [image1 setImage:image];
    

    If you want to have animated photos, try this link.

    Conclusion

    1. To update WKInterfaceLabels, you should call the setText() method on the corresponding label.
    2. To update WKInterfaceImages, you should call setImage() or setImageNamed() methods on the corresponding image view.

    Resources

    1. WKInterfaceImage Class Reference
    2. WKInterfaceLabel Class Reference